ZFPlayerGestureControl.m 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. //
  2. // ZFPlayerGestureControl.m
  3. // ZFPlayer
  4. //
  5. // Copyright (c) 2016年 任子丰 ( http://github.com/renzifeng )
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. #import "ZFPlayerGestureControl.h"
  25. @interface ZFPlayerGestureControl ()<UIGestureRecognizerDelegate>
  26. @property (nonatomic, strong) UITapGestureRecognizer *singleTap;
  27. @property (nonatomic, strong) UITapGestureRecognizer *doubleTap;
  28. @property (nonatomic, strong) UIPanGestureRecognizer *panGR;
  29. @property (nonatomic, strong) UIPinchGestureRecognizer *pinchGR;
  30. @property (nonatomic) ZFPanDirection panDirection;
  31. @property (nonatomic) ZFPanLocation panLocation;
  32. @property (nonatomic) ZFPanMovingDirection panMovingDirection;
  33. @property (nonatomic, weak) UIView *targetView;
  34. @end
  35. @implementation ZFPlayerGestureControl
  36. - (void)addGestureToView:(UIView *)view {
  37. self.targetView = view;
  38. self.targetView.multipleTouchEnabled = YES;
  39. [self.singleTap requireGestureRecognizerToFail:self.doubleTap];
  40. [self.singleTap requireGestureRecognizerToFail:self.panGR];
  41. [self.targetView addGestureRecognizer:self.singleTap];
  42. [self.targetView addGestureRecognizer:self.doubleTap];
  43. [self.targetView addGestureRecognizer:self.panGR];
  44. [self.targetView addGestureRecognizer:self.pinchGR];
  45. }
  46. - (void)removeGestureToView:(UIView *)view {
  47. [view removeGestureRecognizer:self.singleTap];
  48. [view removeGestureRecognizer:self.doubleTap];
  49. [view removeGestureRecognizer:self.panGR];
  50. [view removeGestureRecognizer:self.pinchGR];
  51. }
  52. - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
  53. if (gestureRecognizer == self.panGR) {
  54. CGPoint translation = [(UIPanGestureRecognizer *)gestureRecognizer translationInView:self.targetView];
  55. CGFloat x = fabs(translation.x);
  56. CGFloat y = fabs(translation.y);
  57. if (x < y && self.disablePanMovingDirection & ZFPlayerDisablePanMovingDirectionVertical) { /// up and down moving direction.
  58. return NO;
  59. } else if (x > y && self.disablePanMovingDirection & ZFPlayerDisablePanMovingDirectionHorizontal) { /// left and right moving direction.
  60. return NO;
  61. }
  62. }
  63. return YES;
  64. }
  65. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
  66. ZFPlayerGestureType type = ZFPlayerGestureTypeUnknown;
  67. if (gestureRecognizer == self.singleTap) type = ZFPlayerGestureTypeSingleTap;
  68. else if (gestureRecognizer == self.doubleTap) type = ZFPlayerGestureTypeDoubleTap;
  69. else if (gestureRecognizer == self.panGR) type = ZFPlayerGestureTypePan;
  70. else if (gestureRecognizer == self.pinchGR) type = ZFPlayerGestureTypePinch;
  71. CGPoint locationPoint = [touch locationInView:touch.view];
  72. if (locationPoint.x > _targetView.bounds.size.width / 2) {
  73. self.panLocation = ZFPanLocationRight;
  74. } else {
  75. self.panLocation = ZFPanLocationLeft;
  76. }
  77. switch (type) {
  78. case ZFPlayerGestureTypeUnknown: break;
  79. case ZFPlayerGestureTypePan: {
  80. if (self.disableTypes & ZFPlayerDisableGestureTypesPan) {
  81. return NO;
  82. }
  83. }
  84. break;
  85. case ZFPlayerGestureTypePinch: {
  86. if (self.disableTypes & ZFPlayerDisableGestureTypesPinch) {
  87. return NO;
  88. }
  89. }
  90. break;
  91. case ZFPlayerGestureTypeDoubleTap: {
  92. if (self.disableTypes & ZFPlayerDisableGestureTypesDoubleTap) {
  93. return NO;
  94. }
  95. }
  96. break;
  97. case ZFPlayerGestureTypeSingleTap: {
  98. if (self.disableTypes & ZFPlayerDisableGestureTypesSingleTap) {
  99. return NO;
  100. }
  101. }
  102. break;
  103. }
  104. if (self.triggerCondition) return self.triggerCondition(self, type, gestureRecognizer, touch);
  105. return YES;
  106. }
  107. // Whether to support multi-trigger, return YES, you can trigger a method with multiple gestures, return NO is mutually exclusive
  108. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
  109. if (otherGestureRecognizer != self.singleTap &&
  110. otherGestureRecognizer != self.doubleTap &&
  111. otherGestureRecognizer != self.panGR &&
  112. otherGestureRecognizer != self.pinchGR) return NO;
  113. if (gestureRecognizer == self.panGR) {
  114. CGPoint translation = [(UIPanGestureRecognizer *)gestureRecognizer translationInView:self.targetView];
  115. CGFloat x = fabs(translation.x);
  116. CGFloat y = fabs(translation.y);
  117. if (x < y && self.disablePanMovingDirection & ZFPlayerDisablePanMovingDirectionVertical) {
  118. return YES;
  119. } else if (x > y && self.disablePanMovingDirection & ZFPlayerDisablePanMovingDirectionHorizontal) {
  120. return YES;
  121. }
  122. }
  123. if (gestureRecognizer.numberOfTouches >= 2) {
  124. return NO;
  125. }
  126. return YES;
  127. }
  128. - (UITapGestureRecognizer *)singleTap {
  129. if (!_singleTap){
  130. _singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
  131. _singleTap.delegate = self;
  132. _singleTap.delaysTouchesBegan = YES;
  133. _singleTap.delaysTouchesEnded = YES;
  134. _singleTap.numberOfTouchesRequired = 1;
  135. _singleTap.numberOfTapsRequired = 1;
  136. }
  137. return _singleTap;
  138. }
  139. - (UITapGestureRecognizer *)doubleTap {
  140. if (!_doubleTap) {
  141. _doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
  142. _doubleTap.delegate = self;
  143. _doubleTap.delaysTouchesBegan = YES;
  144. _singleTap.delaysTouchesEnded = YES;
  145. _doubleTap.numberOfTouchesRequired = 1;
  146. _doubleTap.numberOfTapsRequired = 2;
  147. }
  148. return _doubleTap;
  149. }
  150. - (UIPanGestureRecognizer *)panGR {
  151. if (!_panGR) {
  152. _panGR = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(youpaifhandlePan:)];
  153. _panGR.delegate = self;
  154. _panGR.delaysTouchesBegan = YES;
  155. _panGR.delaysTouchesEnded = YES;
  156. _panGR.maximumNumberOfTouches = 1;
  157. _panGR.cancelsTouchesInView = YES;
  158. }
  159. return _panGR;
  160. }
  161. - (UIPinchGestureRecognizer *)pinchGR {
  162. if (!_pinchGR) {
  163. _pinchGR = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)];
  164. _pinchGR.delegate = self;
  165. _pinchGR.delaysTouchesBegan = YES;
  166. }
  167. return _pinchGR;
  168. }
  169. - (void)handleSingleTap:(UITapGestureRecognizer *)tap {
  170. if (self.singleTapped) self.singleTapped(self);
  171. }
  172. - (void)handleDoubleTap:(UITapGestureRecognizer *)tap {
  173. if (self.doubleTapped) self.doubleTapped(self);
  174. }
  175. - (void)youpaifhandlePan:(UIPanGestureRecognizer *)pan {
  176. CGPoint translate = [pan translationInView:pan.view];
  177. CGPoint velocity = [pan velocityInView:pan.view];
  178. switch (pan.state) {
  179. case UIGestureRecognizerStateBegan: {
  180. self.panMovingDirection = ZFPanMovingDirectionUnkown;
  181. CGFloat x = fabs(velocity.x);
  182. CGFloat y = fabs(velocity.y);
  183. if (x > y) {
  184. self.panDirection = ZFPanDirectionH;
  185. } else if (x < y) {
  186. self.panDirection = ZFPanDirectionV;
  187. } else {
  188. self.panDirection = ZFPanDirectionUnknown;
  189. }
  190. if (self.beganPan) self.beganPan(self, self.panDirection, self.panLocation);
  191. }
  192. break;
  193. case UIGestureRecognizerStateChanged: {
  194. switch (_panDirection) {
  195. case ZFPanDirectionH: {
  196. if (translate.x > 0) {
  197. self.panMovingDirection = ZFPanMovingDirectionRight;
  198. } else if (translate.y < 0) {
  199. self.panMovingDirection = ZFPanMovingDirectionLeft;
  200. }
  201. }
  202. break;
  203. case ZFPanDirectionV: {
  204. if (translate.y > 0) {
  205. self.panMovingDirection = ZFPanMovingDirectionBottom;
  206. } else {
  207. self.panMovingDirection = ZFPanMovingDirectionTop;
  208. }
  209. }
  210. break;
  211. case ZFPanDirectionUnknown:
  212. break;
  213. }
  214. if (self.changedPan) self.changedPan(self, self.panDirection, self.panLocation, velocity);
  215. }
  216. break;
  217. case UIGestureRecognizerStateFailed:
  218. case UIGestureRecognizerStateCancelled:
  219. case UIGestureRecognizerStateEnded: {
  220. if (self.endedPan) self.endedPan(self, self.panDirection, self.panLocation);
  221. }
  222. break;
  223. default:
  224. break;
  225. }
  226. [pan setTranslation:CGPointZero inView:pan.view];
  227. }
  228. - (void)handlePinch:(UIPinchGestureRecognizer *)pinch {
  229. switch (pinch.state) {
  230. case UIGestureRecognizerStateEnded: {
  231. if (self.pinched) self.pinched(self, pinch.scale);
  232. }
  233. break;
  234. default:
  235. break;
  236. }
  237. }
  238. @end