123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- //
- // ZFPlayerGestureControl.m
- // ZFPlayer
- //
- // Copyright (c) 2016年 任子丰 ( http://github.com/renzifeng )
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- #import "ZFPlayerGestureControl.h"
- @interface ZFPlayerGestureControl ()<UIGestureRecognizerDelegate>
- @property (nonatomic, strong) UITapGestureRecognizer *singleTap;
- @property (nonatomic, strong) UITapGestureRecognizer *doubleTap;
- @property (nonatomic, strong) UIPanGestureRecognizer *panGR;
- @property (nonatomic, strong) UIPinchGestureRecognizer *pinchGR;
- @property (nonatomic) ZFPanDirection panDirection;
- @property (nonatomic) ZFPanLocation panLocation;
- @property (nonatomic) ZFPanMovingDirection panMovingDirection;
- @property (nonatomic, weak) UIView *targetView;
- @end
- @implementation ZFPlayerGestureControl
- - (void)addGestureToView:(UIView *)view {
- self.targetView = view;
- self.targetView.multipleTouchEnabled = YES;
- [self.singleTap requireGestureRecognizerToFail:self.doubleTap];
- [self.singleTap requireGestureRecognizerToFail:self.panGR];
- [self.targetView addGestureRecognizer:self.singleTap];
- [self.targetView addGestureRecognizer:self.doubleTap];
- [self.targetView addGestureRecognizer:self.panGR];
- [self.targetView addGestureRecognizer:self.pinchGR];
- }
- - (void)removeGestureToView:(UIView *)view {
- [view removeGestureRecognizer:self.singleTap];
- [view removeGestureRecognizer:self.doubleTap];
- [view removeGestureRecognizer:self.panGR];
- [view removeGestureRecognizer:self.pinchGR];
- }
- - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
- if (gestureRecognizer == self.panGR) {
- CGPoint translation = [(UIPanGestureRecognizer *)gestureRecognizer translationInView:self.targetView];
- CGFloat x = fabs(translation.x);
- CGFloat y = fabs(translation.y);
- if (x < y && self.disablePanMovingDirection & ZFPlayerDisablePanMovingDirectionVertical) { /// up and down moving direction.
- return NO;
- } else if (x > y && self.disablePanMovingDirection & ZFPlayerDisablePanMovingDirectionHorizontal) { /// left and right moving direction.
- return NO;
- }
- }
- return YES;
- }
- - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
- ZFPlayerGestureType type = ZFPlayerGestureTypeUnknown;
- if (gestureRecognizer == self.singleTap) type = ZFPlayerGestureTypeSingleTap;
- else if (gestureRecognizer == self.doubleTap) type = ZFPlayerGestureTypeDoubleTap;
- else if (gestureRecognizer == self.panGR) type = ZFPlayerGestureTypePan;
- else if (gestureRecognizer == self.pinchGR) type = ZFPlayerGestureTypePinch;
- CGPoint locationPoint = [touch locationInView:touch.view];
- if (locationPoint.x > _targetView.bounds.size.width / 2) {
- self.panLocation = ZFPanLocationRight;
- } else {
- self.panLocation = ZFPanLocationLeft;
- }
-
- switch (type) {
- case ZFPlayerGestureTypeUnknown: break;
- case ZFPlayerGestureTypePan: {
- if (self.disableTypes & ZFPlayerDisableGestureTypesPan) {
- return NO;
- }
- }
- break;
- case ZFPlayerGestureTypePinch: {
- if (self.disableTypes & ZFPlayerDisableGestureTypesPinch) {
- return NO;
- }
- }
- break;
- case ZFPlayerGestureTypeDoubleTap: {
- if (self.disableTypes & ZFPlayerDisableGestureTypesDoubleTap) {
- return NO;
- }
- }
- break;
- case ZFPlayerGestureTypeSingleTap: {
- if (self.disableTypes & ZFPlayerDisableGestureTypesSingleTap) {
- return NO;
- }
- }
- break;
- }
-
- if (self.triggerCondition) return self.triggerCondition(self, type, gestureRecognizer, touch);
- return YES;
- }
- // Whether to support multi-trigger, return YES, you can trigger a method with multiple gestures, return NO is mutually exclusive
- - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
- if (otherGestureRecognizer != self.singleTap &&
- otherGestureRecognizer != self.doubleTap &&
- otherGestureRecognizer != self.panGR &&
- otherGestureRecognizer != self.pinchGR) return NO;
-
- if (gestureRecognizer == self.panGR) {
- CGPoint translation = [(UIPanGestureRecognizer *)gestureRecognizer translationInView:self.targetView];
- CGFloat x = fabs(translation.x);
- CGFloat y = fabs(translation.y);
- if (x < y && self.disablePanMovingDirection & ZFPlayerDisablePanMovingDirectionVertical) {
- return YES;
- } else if (x > y && self.disablePanMovingDirection & ZFPlayerDisablePanMovingDirectionHorizontal) {
- return YES;
- }
- }
- if (gestureRecognizer.numberOfTouches >= 2) {
- return NO;
- }
- return YES;
- }
- - (UITapGestureRecognizer *)singleTap {
- if (!_singleTap){
- _singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
- _singleTap.delegate = self;
- _singleTap.delaysTouchesBegan = YES;
- _singleTap.delaysTouchesEnded = YES;
- _singleTap.numberOfTouchesRequired = 1;
- _singleTap.numberOfTapsRequired = 1;
- }
- return _singleTap;
- }
- - (UITapGestureRecognizer *)doubleTap {
- if (!_doubleTap) {
- _doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
- _doubleTap.delegate = self;
- _doubleTap.delaysTouchesBegan = YES;
- _singleTap.delaysTouchesEnded = YES;
- _doubleTap.numberOfTouchesRequired = 1;
- _doubleTap.numberOfTapsRequired = 2;
- }
- return _doubleTap;
- }
- - (UIPanGestureRecognizer *)panGR {
- if (!_panGR) {
- _panGR = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(youpaifhandlePan:)];
- _panGR.delegate = self;
- _panGR.delaysTouchesBegan = YES;
- _panGR.delaysTouchesEnded = YES;
- _panGR.maximumNumberOfTouches = 1;
- _panGR.cancelsTouchesInView = YES;
- }
- return _panGR;
- }
- - (UIPinchGestureRecognizer *)pinchGR {
- if (!_pinchGR) {
- _pinchGR = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)];
- _pinchGR.delegate = self;
- _pinchGR.delaysTouchesBegan = YES;
- }
- return _pinchGR;
- }
- - (void)handleSingleTap:(UITapGestureRecognizer *)tap {
- if (self.singleTapped) self.singleTapped(self);
- }
- - (void)handleDoubleTap:(UITapGestureRecognizer *)tap {
- if (self.doubleTapped) self.doubleTapped(self);
- }
- - (void)youpaifhandlePan:(UIPanGestureRecognizer *)pan {
- CGPoint translate = [pan translationInView:pan.view];
- CGPoint velocity = [pan velocityInView:pan.view];
- switch (pan.state) {
- case UIGestureRecognizerStateBegan: {
- self.panMovingDirection = ZFPanMovingDirectionUnkown;
- CGFloat x = fabs(velocity.x);
- CGFloat y = fabs(velocity.y);
- if (x > y) {
- self.panDirection = ZFPanDirectionH;
- } else if (x < y) {
- self.panDirection = ZFPanDirectionV;
- } else {
- self.panDirection = ZFPanDirectionUnknown;
- }
-
- if (self.beganPan) self.beganPan(self, self.panDirection, self.panLocation);
- }
- break;
- case UIGestureRecognizerStateChanged: {
- switch (_panDirection) {
- case ZFPanDirectionH: {
- if (translate.x > 0) {
- self.panMovingDirection = ZFPanMovingDirectionRight;
- } else if (translate.y < 0) {
- self.panMovingDirection = ZFPanMovingDirectionLeft;
- }
- }
- break;
- case ZFPanDirectionV: {
- if (translate.y > 0) {
- self.panMovingDirection = ZFPanMovingDirectionBottom;
- } else {
- self.panMovingDirection = ZFPanMovingDirectionTop;
- }
- }
- break;
- case ZFPanDirectionUnknown:
- break;
- }
- if (self.changedPan) self.changedPan(self, self.panDirection, self.panLocation, velocity);
- }
- break;
- case UIGestureRecognizerStateFailed:
- case UIGestureRecognizerStateCancelled:
- case UIGestureRecognizerStateEnded: {
- if (self.endedPan) self.endedPan(self, self.panDirection, self.panLocation);
- }
- break;
- default:
- break;
- }
- [pan setTranslation:CGPointZero inView:pan.view];
- }
- - (void)handlePinch:(UIPinchGestureRecognizer *)pinch {
- switch (pinch.state) {
- case UIGestureRecognizerStateEnded: {
- if (self.pinched) self.pinched(self, pinch.scale);
- }
- break;
- default:
- break;
- }
- }
- @end
|