DZMCoverController.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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.pan.delaysTouchesBegan = YES;
  63. self.pan.delaysTouchesEnded = YES;
  64. [self.view addGestureRecognizer:self.pan];
  65. // 启用手势
  66. self.gestureRecognizerEnabled = YES;
  67. // 开启裁剪
  68. self.view.layer.masksToBounds = YES;
  69. }
  70. -(void)setIsCanTap:(BOOL)isCanTap{
  71. _isCanTap = isCanTap;
  72. if (isCanTap) {
  73. self.tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touchTap:)];
  74. [self.view addGestureRecognizer:self.tap];
  75. self.tap.delegate = self;
  76. }else{
  77. [self.view removeGestureRecognizer:self.tap];
  78. }
  79. }
  80. /**
  81. * 手势开关
  82. */
  83. - (void)setGestureRecognizerEnabled:(BOOL)gestureRecognizerEnabled
  84. {
  85. _gestureRecognizerEnabled = gestureRecognizerEnabled;
  86. self.pan.enabled = gestureRecognizerEnabled;
  87. // self.tap.enabled = gestureRecognizerEnabled;
  88. }
  89. #pragma mark - 手势处理
  90. - (void)touchPan:(UIPanGestureRecognizer *)pan
  91. {
  92. // 用于辨别方向
  93. CGPoint tempPoint = [pan translationInView:self.view];
  94. // 用于计算位置
  95. CGPoint touchPoint = [pan locationInView:self.view];
  96. if (pan.state == UIGestureRecognizerStateBegan) { // 手势开始
  97. // 正在动画
  98. if (self.isAnimateChange) { return; }
  99. self.isAnimateChange = YES;
  100. self.isPanBegin = YES;
  101. self.isPan = YES;
  102. }else if (pan.state == UIGestureRecognizerStateChanged) { // 手势移动
  103. if (fabs(tempPoint.x) > 0.01) { // 滚动有值了
  104. // 获取将要显示的控制器
  105. if (self.isPanBegin) {
  106. self.isPanBegin = NO;
  107. // 获取将要显示的控制器
  108. self.pendingController = [self GetPanControllerWithTouchPoint:tempPoint];
  109. // 将要显示的控制器
  110. if ([self.delegate respondsToSelector:@selector(coverController:willTransitionToPendingController:)]) {
  111. [self.delegate coverController:self willTransitionToPendingController:self.pendingController];
  112. }
  113. // 添加
  114. [self addController:self.pendingController];
  115. }
  116. // 判断显示
  117. if (self.openAnimate && self.isPan) {
  118. if (self.pendingController) {
  119. if (self.isLeft) {
  120. self.pendingController.view.frame = CGRectMake(touchPoint.x - ViewWidth, 0, ViewWidth, ViewHeight);
  121. }else{
  122. self.currentController.view.frame = CGRectMake(tempPoint.x > 0 ? 0 : tempPoint.x, 0, ViewWidth, ViewHeight);
  123. }
  124. }
  125. }
  126. }
  127. }else{ // 手势结束
  128. if (self.isPan) {
  129. // 结束Pan手势
  130. self.isPan = NO;
  131. if (self.openAnimate) { // 动画
  132. if (self.pendingController) {
  133. BOOL isSuccess = YES;
  134. if (self.isLeft) {
  135. if (self.pendingController.view.frame.origin.x <= -(ViewWidth - ViewWidth*0.18)) {
  136. isSuccess = NO;
  137. }
  138. }else{
  139. if (self.currentController.view.frame.origin.x >= -1) {
  140. isSuccess = NO;
  141. }
  142. }
  143. // 手势结束
  144. [self GestureSuccess:isSuccess animated:YES];
  145. }else{
  146. self.isAnimateChange = NO;
  147. }
  148. }else{ // 无动画
  149. // 手势结束
  150. [self GestureSuccess:YES animated:self.openAnimate];
  151. }
  152. }
  153. }
  154. }
  155. - (void)touchTap:(UITapGestureRecognizer *)tap
  156. {
  157. // 正在动画
  158. if (self.isAnimateChange) { return; }
  159. self.isAnimateChange = YES;
  160. CGPoint touchPoint = [tap locationInView:self.view];
  161. // 获取将要显示的控制器
  162. self.pendingController = [self GetTapControllerWithTouchPoint:touchPoint];
  163. // 将要显示的控制器
  164. if ([self.delegate respondsToSelector:@selector(coverController:willTransitionToPendingController:)]) {
  165. [self.delegate coverController:self willTransitionToPendingController:self.pendingController];
  166. }
  167. // 添加
  168. [self addController:self.pendingController];
  169. // 手势结束
  170. [self GestureSuccess:YES animated:self.openAnimate];
  171. }
  172. /**
  173. * 手势结束
  174. */
  175. - (void)GestureSuccess:(BOOL)isSuccess animated:(BOOL)animated {
  176. @weakify(self)
  177. if (self.pendingController) {
  178. if (self.isLeft) { // 左边
  179. if (animated) {
  180. [UIView qmui_animateWithAnimated:YES duration:AnimateDuration delay:0 options:UIViewAnimationOptionAllowAnimatedContent animations:^{
  181. @strongify(self)
  182. if (isSuccess) {
  183. self.pendingController.view.frame = CGRectMake(0, 0, ViewWidth, ViewHeight);
  184. } else {
  185. self.pendingController.view.frame = CGRectMake(-ViewWidth, 0, ViewWidth, ViewHeight);
  186. }
  187. } completion:^(BOOL finished) {
  188. @strongify(self)
  189. [self animateSuccess:isSuccess];
  190. }];
  191. } else {
  192. if (isSuccess) {
  193. self.pendingController.view.frame = CGRectMake(0, 0, ViewWidth, ViewHeight);
  194. } else {
  195. self.pendingController.view.frame = CGRectMake(-ViewWidth, 0, ViewWidth, ViewHeight);
  196. }
  197. [self animateSuccess:isSuccess];
  198. }
  199. } else { // 右边
  200. if (animated) {
  201. [UIView qmui_animateWithAnimated:YES duration:AnimateDuration delay:0 options:UIViewAnimationOptionAllowAnimatedContent animations:^{
  202. @strongify(self)
  203. if (isSuccess) {
  204. self.currentController.view.frame = CGRectMake(-ViewWidth, 0, ViewWidth, ViewHeight);
  205. } else {
  206. self.currentController.view.frame = CGRectMake(0, 0, ViewWidth, ViewHeight);
  207. }
  208. } completion:^(BOOL finished) {
  209. @strongify(self)
  210. [self animateSuccess:isSuccess];
  211. }];
  212. } else {
  213. if (isSuccess) {
  214. self.currentController.view.frame = CGRectMake(-ViewWidth, 0, ViewWidth, ViewHeight);
  215. } else {
  216. self.currentController.view.frame = CGRectMake(0, 0, ViewWidth, ViewHeight);
  217. }
  218. [self animateSuccess:isSuccess];
  219. }
  220. }
  221. }
  222. }
  223. /**
  224. * 动画结束
  225. */
  226. - (void)animateSuccess:(BOOL)isSuccess
  227. {
  228. if (isSuccess) {
  229. [self.currentController.view removeFromSuperview];
  230. [self.currentController removeFromParentViewController];
  231. _currentController = self.pendingController;
  232. self.pendingController = nil;
  233. self.isAnimateChange = NO;
  234. }else{
  235. [self.pendingController.view removeFromSuperview];
  236. [self.pendingController removeFromParentViewController];
  237. self.pendingController = nil;
  238. self.isAnimateChange = NO;
  239. }
  240. // 代理回调
  241. if ([self.delegate respondsToSelector:@selector(coverController:currentController:finish:)]) {
  242. [self.delegate coverController:self currentController:self.currentController finish:isSuccess];
  243. }
  244. }
  245. /**
  246. * 根据手势触发的位置获取控制器
  247. *
  248. * @param touchPoint 手势触发位置
  249. *
  250. * @return 需要显示的控制器
  251. */
  252. - (UIViewController * _Nullable)GetTapControllerWithTouchPoint:(CGPoint)touchPoint
  253. {
  254. UIViewController *vc = nil;
  255. if (touchPoint.x < ViewWidth / 3) { // 左边
  256. self.isLeft = YES;
  257. // 获取上一个显示控制器
  258. if ([self.delegate respondsToSelector:@selector(coverController:getAboveControllerWithCurrentController:)]) {
  259. vc = [self.delegate coverController:self getAboveControllerWithCurrentController:self.currentController];
  260. }
  261. }else if (touchPoint.x > (ViewWidth - ViewWidth / 3)){ // 右边
  262. self.isLeft = NO;
  263. // 获取下一个显示控制器
  264. if ([self.delegate respondsToSelector:@selector(coverController:getBelowControllerWithCurrentController:)]) {
  265. vc = [self.delegate coverController:self getBelowControllerWithCurrentController:self.currentController];
  266. }
  267. }
  268. if (!vc) {
  269. self.isAnimateChange = NO;
  270. }
  271. return vc;
  272. }
  273. /**
  274. * 根据手势触发的位置获取控制器
  275. *
  276. * @param touchPoint 手势触发位置
  277. *
  278. * @return 需要显示的控制器
  279. */
  280. - (UIViewController * _Nullable)GetPanControllerWithTouchPoint:(CGPoint)touchPoint
  281. {
  282. UIViewController *vc = nil;
  283. if (touchPoint.x > 0) { // 左边
  284. self.isLeft = YES;
  285. // 获取上一个显示控制器
  286. if ([self.delegate respondsToSelector:@selector(coverController:getAboveControllerWithCurrentController:)]) {
  287. vc = [self.delegate coverController:self getAboveControllerWithCurrentController:self.currentController];
  288. }
  289. }else{ // 右边
  290. self.isLeft = NO;
  291. // 获取下一个显示控制器
  292. if ([self.delegate respondsToSelector:@selector(coverController:getBelowControllerWithCurrentController:)]) {
  293. vc = [self.delegate coverController:self getBelowControllerWithCurrentController:self.currentController];
  294. }
  295. }
  296. if (!vc) {
  297. self.isAnimateChange = NO;
  298. }
  299. return vc;
  300. }
  301. #pragma mark - 设置显示控制器
  302. /**
  303. * 手动设置显示控制器 无动画
  304. *
  305. * @param controller 设置显示的控制器
  306. */
  307. - (void)setController:(UIViewController * _Nullable)controller
  308. {
  309. [self setController:controller animated:NO isAbove:YES];
  310. }
  311. /**
  312. * 手动设置显示控制器
  313. *
  314. * @param controller 设置显示的控制器
  315. * @param animated 是否需要动画
  316. * @param isAbove 动画是否从上面显示 YES 从下面显示 NO
  317. */
  318. - (void)setController:(UIViewController * _Nullable)controller animated:(BOOL)animated isAbove:(BOOL)isAbove
  319. {
  320. if (controller) { // 有值
  321. if (animated && self.currentController) { // 需要动画 同时有根控制器了
  322. // 正在动画
  323. if (self.isAnimateChange) { return; }
  324. self.isAnimateChange = YES;
  325. self.isLeft = isAbove;
  326. // 记录
  327. self.pendingController = controller;
  328. // 添加
  329. [self addController:controller];
  330. // 手势结束
  331. [self GestureSuccess:YES animated:YES];
  332. }else{
  333. // 添加
  334. [self addController:controller];
  335. // 修改frame
  336. controller.view.frame = self.view.bounds;
  337. // 当前控制器有值 进行删除
  338. if (_currentController) {
  339. [_currentController.view removeFromSuperview];
  340. [_currentController removeFromParentViewController];
  341. }
  342. // 赋值记录
  343. _currentController = controller;
  344. }
  345. }
  346. }
  347. /**
  348. * 添加控制器
  349. *
  350. * @param controller 控制器
  351. */
  352. - (void)addController:(UIViewController * _Nullable)controller
  353. {
  354. if (controller) {
  355. [self addChildViewController:controller];
  356. if (self.isLeft) { // 左边
  357. [self.view addSubview:controller.view];
  358. controller.view.frame = CGRectMake(-ViewWidth, 0, ViewWidth, ViewHeight);
  359. }else{ // 右边
  360. if (self.currentController) { // 有值
  361. [self.view insertSubview:controller.view belowSubview:self.currentController.view];
  362. }else{ // 没值
  363. [self.view addSubview:controller.view];
  364. }
  365. controller.view.frame = CGRectMake(0, 0, ViewWidth, ViewHeight);
  366. }
  367. // 添加阴影
  368. [self setShadowController:controller];
  369. }
  370. }
  371. /**
  372. * 给控制器添加阴影
  373. */
  374. - (void)setShadowController:(UIViewController * _Nullable)controller
  375. {
  376. if (controller) {
  377. controller.view.layer.shadowColor = [UIColor blackColor].CGColor; // 阴影颜色
  378. controller.view.layer.shadowOffset = CGSizeMake(0, 0); // 偏移距离
  379. controller.view.layer.shadowOpacity = 0.5; // 不透明度
  380. controller.view.layer.shadowRadius = 10.0; // 半径
  381. }
  382. }
  383. #pragma mark - UIGestureRecognizerDelegate
  384. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
  385. {
  386. if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]] && [gestureRecognizer isEqual:self.tap]) {
  387. CGFloat tempX = ViewWidth / 3;
  388. CGPoint touchPoint = [self.tap locationInView:self.view];
  389. if (touchPoint.x > tempX && touchPoint.x < (ViewWidth - tempX)) {
  390. return YES;
  391. }
  392. }
  393. return NO;
  394. }
  395. - (void)didReceiveMemoryWarning {
  396. [super didReceiveMemoryWarning];
  397. }
  398. - (void)dealloc {
  399. // 移除手势
  400. [self.view removeGestureRecognizer:self.pan];
  401. [self.view removeGestureRecognizer:self.tap];
  402. // 移除当前控制器
  403. if (self.currentController) {
  404. [self.currentController.view removeFromSuperview];
  405. [self.currentController removeFromParentViewController];
  406. _currentController = nil;
  407. }
  408. // 移除临时控制器
  409. if (self.pendingController) {
  410. [self.pendingController.view removeFromSuperview];
  411. [self.pendingController removeFromParentViewController];
  412. self.pendingController = nil;
  413. }
  414. }
  415. @end