DZMCoverController.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. //
  2. // DZMCoverController.m
  3. // DZMCoverDemo
  4. //
  5. // Created by 邓泽淼 on 16/10/8.
  6. // Copyright © 2016年 DZM. All rights reserved.
  7. //
  8. // View宽
  9. #define ViewWidth self.view.frame.size.width
  10. // View高
  11. #define ViewHeight self.view.frame.size.height
  12. // 动画时间
  13. #define AnimateDuration 0.30
  14. #import "DZMCoverController.h"
  15. @interface DZMCoverController ()<UIGestureRecognizerDelegate>
  16. /**
  17. * 左拉右拉手势
  18. */
  19. @property (nonatomic,strong) UIPanGestureRecognizer *pan;
  20. /**
  21. * 点击手势
  22. */
  23. @property (nonatomic,strong) UITapGestureRecognizer *tap;
  24. /**
  25. * 手势触发点在左边 辨认方向 左边拿上一个控制器 右边拿下一个控制器
  26. */
  27. @property (nonatomic,assign) BOOL isLeft;
  28. /**
  29. * 判断执行pan手势
  30. */
  31. @property (nonatomic,assign) BOOL isPan;
  32. /**
  33. * 手势是否重新开始识别
  34. */
  35. @property (nonatomic,assign) BOOL isPanBegin;
  36. /**
  37. * 动画状态
  38. */
  39. @property (nonatomic,assign) BOOL isAnimateChange;
  40. /**
  41. * 临时控制器 通过代理获取回来的控制器 还没有完全展示出来的控制器
  42. */
  43. @property (nonatomic,strong,nullable) UIViewController *pendingController;
  44. @end
  45. @implementation DZMCoverController
  46. - (void)viewDidLoad {
  47. [super viewDidLoad];
  48. // 完成初始化
  49. [self didInit];
  50. }
  51. /**
  52. * 初始化
  53. */
  54. - (void)didInit
  55. {
  56. // 动画效果开启
  57. self.openAnimate = YES;
  58. // 设置背景颜色
  59. self.view.backgroundColor = [UIColor whiteColor];
  60. // 添加手势
  61. self.pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(touchPan:)];
  62. [self.view addGestureRecognizer:self.pan];
  63. // 启用手势
  64. self.gestureRecognizerEnabled = YES;
  65. // 开启裁剪
  66. self.view.layer.masksToBounds = YES;
  67. }
  68. -(void)setIsCanTap:(BOOL)isCanTap{
  69. _isCanTap = isCanTap;
  70. if (isCanTap) {
  71. self.tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touchTap:)];
  72. [self.view addGestureRecognizer:self.tap];
  73. self.tap.delegate = self;
  74. }else{
  75. [self.view removeGestureRecognizer:self.tap];
  76. }
  77. }
  78. /**
  79. * 手势开关
  80. */
  81. - (void)setGestureRecognizerEnabled:(BOOL)gestureRecognizerEnabled
  82. {
  83. _gestureRecognizerEnabled = gestureRecognizerEnabled;
  84. self.pan.enabled = gestureRecognizerEnabled;
  85. // self.tap.enabled = gestureRecognizerEnabled;
  86. }
  87. #pragma mark - 手势处理
  88. - (void)touchPan:(UIPanGestureRecognizer *)pan
  89. {
  90. // 用于辨别方向
  91. CGPoint tempPoint = [pan translationInView:self.view];
  92. // 用于计算位置
  93. CGPoint touchPoint = [pan locationInView:self.view];
  94. if (pan.state == UIGestureRecognizerStateBegan) { // 手势开始
  95. // 正在动画
  96. if (self.isAnimateChange) { return; }
  97. self.isAnimateChange = YES;
  98. self.isPanBegin = YES;
  99. self.isPan = YES;
  100. }else if (pan.state == UIGestureRecognizerStateChanged) { // 手势移动
  101. if (fabs(tempPoint.x) > 0.01) { // 滚动有值了
  102. // 获取将要显示的控制器
  103. if (self.isPanBegin) {
  104. self.isPanBegin = NO;
  105. // 获取将要显示的控制器
  106. self.pendingController = [self GetPanControllerWithTouchPoint:tempPoint];
  107. // 将要显示的控制器
  108. if ([self.delegate respondsToSelector:@selector(coverController:willTransitionToPendingController:)]) {
  109. [self.delegate coverController:self willTransitionToPendingController:self.pendingController];
  110. }
  111. // 添加
  112. [self addController:self.pendingController];
  113. }
  114. // 判断显示
  115. if (self.openAnimate && self.isPan) {
  116. if (self.pendingController) {
  117. if (self.isLeft) {
  118. self.pendingController.view.frame = CGRectMake(touchPoint.x - ViewWidth, 0, ViewWidth, ViewHeight);
  119. }else{
  120. self.currentController.view.frame = CGRectMake(tempPoint.x > 0 ? 0 : tempPoint.x, 0, ViewWidth, ViewHeight);
  121. }
  122. }
  123. }
  124. }
  125. }else{ // 手势结束
  126. if (self.isPan) {
  127. // 结束Pan手势
  128. self.isPan = NO;
  129. if (self.openAnimate) { // 动画
  130. if (self.pendingController) {
  131. BOOL isSuccess = YES;
  132. if (self.isLeft) {
  133. if (self.pendingController.view.frame.origin.x <= -(ViewWidth - ViewWidth*0.18)) {
  134. isSuccess = NO;
  135. }
  136. }else{
  137. if (self.currentController.view.frame.origin.x >= -1) {
  138. isSuccess = NO;
  139. }
  140. }
  141. // 手势结束
  142. [self GestureSuccess:isSuccess animated:YES];
  143. }else{
  144. self.isAnimateChange = NO;
  145. }
  146. }else{ // 无动画
  147. // 手势结束
  148. [self GestureSuccess:YES animated:self.openAnimate];
  149. }
  150. }
  151. }
  152. }
  153. - (void)touchTap:(UITapGestureRecognizer *)tap
  154. {
  155. // 正在动画
  156. if (self.isAnimateChange) { return; }
  157. self.isAnimateChange = YES;
  158. CGPoint touchPoint = [tap locationInView:self.view];
  159. // 获取将要显示的控制器
  160. self.pendingController = [self GetTapControllerWithTouchPoint:touchPoint];
  161. // 将要显示的控制器
  162. if ([self.delegate respondsToSelector:@selector(coverController:willTransitionToPendingController:)]) {
  163. [self.delegate coverController:self willTransitionToPendingController:self.pendingController];
  164. }
  165. // 添加
  166. [self addController:self.pendingController];
  167. // 手势结束
  168. [self GestureSuccess:YES animated:self.openAnimate];
  169. }
  170. /**
  171. * 手势结束
  172. */
  173. - (void)GestureSuccess:(BOOL)isSuccess animated:(BOOL)animated
  174. {
  175. if (self.pendingController) {
  176. if (self.isLeft) { // 左边
  177. if (animated) {
  178. __weak DZMCoverController *weakSelf = self;
  179. [UIView animateWithDuration:AnimateDuration animations:^{
  180. if (isSuccess) {
  181. weakSelf.pendingController.view.frame = CGRectMake(0, 0, ViewWidth, ViewHeight);
  182. }else{
  183. weakSelf.pendingController.view.frame = CGRectMake(-ViewWidth, 0, ViewWidth, ViewHeight);
  184. }
  185. } completion:^(BOOL finished) {
  186. [weakSelf animateSuccess:isSuccess];
  187. }];
  188. }else{
  189. if (isSuccess) {
  190. self.pendingController.view.frame = CGRectMake(0, 0, ViewWidth, ViewHeight);
  191. }else{
  192. self.pendingController.view.frame = CGRectMake(-ViewWidth, 0, ViewWidth, ViewHeight);
  193. }
  194. [self animateSuccess:isSuccess];
  195. }
  196. }else{ // 右边
  197. if (animated) {
  198. __weak DZMCoverController *weakSelf = self;
  199. [UIView animateWithDuration:AnimateDuration animations:^{
  200. if (isSuccess) {
  201. weakSelf.currentController.view.frame = CGRectMake(-ViewWidth, 0, ViewWidth, ViewHeight);
  202. }else{
  203. weakSelf.currentController.view.frame = CGRectMake(0, 0, ViewWidth, ViewHeight);
  204. }
  205. } completion:^(BOOL finished) {
  206. [weakSelf animateSuccess:isSuccess];
  207. }];
  208. }else{
  209. if (isSuccess) {
  210. self.currentController.view.frame = CGRectMake(-ViewWidth, 0, ViewWidth, ViewHeight);
  211. }else{
  212. self.currentController.view.frame = CGRectMake(0, 0, ViewWidth, ViewHeight);
  213. }
  214. [self animateSuccess:isSuccess];
  215. }
  216. }
  217. }
  218. }
  219. /**
  220. * 动画结束
  221. */
  222. - (void)animateSuccess:(BOOL)isSuccess
  223. {
  224. if (isSuccess) {
  225. [self.currentController.view removeFromSuperview];
  226. [self.currentController removeFromParentViewController];
  227. _currentController = self.pendingController;
  228. self.pendingController = nil;
  229. self.isAnimateChange = NO;
  230. }else{
  231. [self.pendingController.view removeFromSuperview];
  232. [self.pendingController removeFromParentViewController];
  233. self.pendingController = nil;
  234. self.isAnimateChange = NO;
  235. }
  236. // 代理回调
  237. if ([self.delegate respondsToSelector:@selector(coverController:currentController:finish:)]) {
  238. [self.delegate coverController:self currentController:self.currentController finish:isSuccess];
  239. }
  240. }
  241. /**
  242. * 根据手势触发的位置获取控制器
  243. *
  244. * @param touchPoint 手势触发位置
  245. *
  246. * @return 需要显示的控制器
  247. */
  248. - (UIViewController * _Nullable)GetTapControllerWithTouchPoint:(CGPoint)touchPoint
  249. {
  250. UIViewController *vc = nil;
  251. if (touchPoint.x < ViewWidth / 3) { // 左边
  252. self.isLeft = YES;
  253. // 获取上一个显示控制器
  254. if ([self.delegate respondsToSelector:@selector(coverController:getAboveControllerWithCurrentController:)]) {
  255. vc = [self.delegate coverController:self getAboveControllerWithCurrentController:self.currentController];
  256. }
  257. }else if (touchPoint.x > (ViewWidth - ViewWidth / 3)){ // 右边
  258. self.isLeft = NO;
  259. // 获取下一个显示控制器
  260. if ([self.delegate respondsToSelector:@selector(coverController:getBelowControllerWithCurrentController:)]) {
  261. vc = [self.delegate coverController:self getBelowControllerWithCurrentController:self.currentController];
  262. }
  263. }
  264. if (!vc) {
  265. self.isAnimateChange = NO;
  266. }
  267. return vc;
  268. }
  269. /**
  270. * 根据手势触发的位置获取控制器
  271. *
  272. * @param touchPoint 手势触发位置
  273. *
  274. * @return 需要显示的控制器
  275. */
  276. - (UIViewController * _Nullable)GetPanControllerWithTouchPoint:(CGPoint)touchPoint
  277. {
  278. UIViewController *vc = nil;
  279. if (touchPoint.x > 0) { // 左边
  280. self.isLeft = YES;
  281. // 获取上一个显示控制器
  282. if ([self.delegate respondsToSelector:@selector(coverController:getAboveControllerWithCurrentController:)]) {
  283. vc = [self.delegate coverController:self getAboveControllerWithCurrentController:self.currentController];
  284. }
  285. }else{ // 右边
  286. self.isLeft = NO;
  287. // 获取下一个显示控制器
  288. if ([self.delegate respondsToSelector:@selector(coverController:getBelowControllerWithCurrentController:)]) {
  289. vc = [self.delegate coverController:self getBelowControllerWithCurrentController:self.currentController];
  290. }
  291. }
  292. if (!vc) {
  293. self.isAnimateChange = NO;
  294. }
  295. return vc;
  296. }
  297. #pragma mark - 设置显示控制器
  298. /**
  299. * 手动设置显示控制器 无动画
  300. *
  301. * @param controller 设置显示的控制器
  302. */
  303. - (void)setController:(UIViewController * _Nullable)controller
  304. {
  305. [self setController:controller animated:NO isAbove:YES];
  306. }
  307. /**
  308. * 手动设置显示控制器
  309. *
  310. * @param controller 设置显示的控制器
  311. * @param animated 是否需要动画
  312. * @param isAbove 动画是否从上面显示 YES 从下面显示 NO
  313. */
  314. - (void)setController:(UIViewController * _Nullable)controller animated:(BOOL)animated isAbove:(BOOL)isAbove
  315. {
  316. if (controller) { // 有值
  317. if (animated && self.currentController) { // 需要动画 同时有根控制器了
  318. // 正在动画
  319. if (self.isAnimateChange) { return; }
  320. self.isAnimateChange = YES;
  321. self.isLeft = isAbove;
  322. // 记录
  323. self.pendingController = controller;
  324. // 添加
  325. [self addController:controller];
  326. // 手势结束
  327. [self GestureSuccess:YES animated:YES];
  328. }else{
  329. // 添加
  330. [self addController:controller];
  331. // 修改frame
  332. controller.view.frame = self.view.bounds;
  333. // 当前控制器有值 进行删除
  334. if (_currentController) {
  335. [_currentController.view removeFromSuperview];
  336. [_currentController removeFromParentViewController];
  337. }
  338. // 赋值记录
  339. _currentController = controller;
  340. }
  341. }
  342. }
  343. /**
  344. * 添加控制器
  345. *
  346. * @param controller 控制器
  347. */
  348. - (void)addController:(UIViewController * _Nullable)controller
  349. {
  350. if (controller) {
  351. [self addChildViewController:controller];
  352. if (self.isLeft) { // 左边
  353. [self.view addSubview:controller.view];
  354. controller.view.frame = CGRectMake(-ViewWidth, 0, ViewWidth, ViewHeight);
  355. }else{ // 右边
  356. if (self.currentController) { // 有值
  357. [self.view insertSubview:controller.view belowSubview:self.currentController.view];
  358. }else{ // 没值
  359. [self.view addSubview:controller.view];
  360. }
  361. controller.view.frame = CGRectMake(0, 0, ViewWidth, ViewHeight);
  362. }
  363. // 添加阴影
  364. [self setShadowController:controller];
  365. }
  366. }
  367. /**
  368. * 给控制器添加阴影
  369. */
  370. - (void)setShadowController:(UIViewController * _Nullable)controller
  371. {
  372. if (controller) {
  373. controller.view.layer.shadowColor = [UIColor blackColor].CGColor; // 阴影颜色
  374. controller.view.layer.shadowOffset = CGSizeMake(0, 0); // 偏移距离
  375. controller.view.layer.shadowOpacity = 0.5; // 不透明度
  376. controller.view.layer.shadowRadius = 10.0; // 半径
  377. }
  378. }
  379. #pragma mark - UIGestureRecognizerDelegate
  380. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
  381. {
  382. if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]] && [gestureRecognizer isEqual:self.tap]) {
  383. CGFloat tempX = ViewWidth / 3;
  384. CGPoint touchPoint = [self.tap locationInView:self.view];
  385. if (touchPoint.x > tempX && touchPoint.x < (ViewWidth - tempX)) {
  386. return YES;
  387. }
  388. }
  389. return NO;
  390. }
  391. - (void)didReceiveMemoryWarning {
  392. [super didReceiveMemoryWarning];
  393. }
  394. - (void)dealloc {
  395. // 移除手势
  396. [self.view removeGestureRecognizer:self.pan];
  397. [self.view removeGestureRecognizer:self.tap];
  398. // 移除当前控制器
  399. if (self.currentController) {
  400. [self.currentController.view removeFromSuperview];
  401. [self.currentController removeFromParentViewController];
  402. _currentController = nil;
  403. }
  404. // 移除临时控制器
  405. if (self.pendingController) {
  406. [self.pendingController.view removeFromSuperview];
  407. [self.pendingController removeFromParentViewController];
  408. self.pendingController = nil;
  409. }
  410. }
  411. @end