UIGestureRecognizer explain

Do you really know UIGestureRecognizer?

Today in the package as a function of time, met the recognition problem between multiple gestures, you need to implement the [gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:] method in the UIGestureRecognizerDelegate protocol to achieve a common recognition of gesture recognition. Therefore, I also take down the relevant knowledge about UIGestureRecognizer.

UIGestureRecognizer explain

I. definition of UIGestureRecognizer

UIGestureRecognizer is an abstract base class that defines the programming interface that implements the underlying gesture recognition behavior so that the subclass that inherits it can handle specific gestures.

The current state of the typedef NS_ENUM / gesture (NSInteger, UIGestureRecognizerState) {/ / not identify what kind of gestures (but may have triggered the touch event), the default UIGestureRecognizerStatePossible / gesture has begun, at this time have been identified, but the process may change, the gesture operation has not been completed UIGestureRecognizerStateBegan / gesture changes state UIGestureRecognizerStateChanged. / / gesture recognition operation is completed (now release the finger) UIGestureRecognizerStateEnded / gesture was canceled, return to the default state of UIGestureRecognizerStateCancelled / / gesture recognition fails, revert to the default state of UIGestureRecognizerStateFailed / / gesture recognition is completed With end, UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded};

@interface UIGestureRecognizer: NSObject / / create a gesture object and add the trigger event - (instancetype) initWithTarget: (nullable ID) target action: (nullable SEL) action NS_DESIGNATED_INITIALIZER; / / give a gesture object listener event - (void) addTarget: (ID) target action: action (SEL); / / remove a gesture of listening to events - (void) removeTarget: (nullable ID) target action: (nullable SEL action); / / get the current state of @property (nonatomic, readonly sign UIGestureRecognizerState state); / / @property (nullable, nonatomic, commissioned by the weak ID <); UIGestureRecognizerDelegate> delegate; / / gesture recognition is available @property (nonatomic, getter=isEnabled) BOOL enabled; / / get View view @property touch gestures read-only (nullable, nonatomic, readonly) UI View *view; / / whether to cancel the response to the touch control defaults to YES, in this case when the gesture recognizer recognizes touch, will send a touchesCancelled to the touch control to cancel the response control of view to touch, this time only gesture recognizer response touch, when set to NO, the gesture recognizer to touch recognition can send touchesCancelled to control the time of gesture recognition and control view response touch. Note: gesture recognition and touch events exist at the same time, just because the touchesCancelled lead to touch event failure (nonatomic), @property BOOL cancelsTouchesInView; / / whether delay sending touch events to touch the default is NO, this situation occurs when a touch, gesture recognizer to capture the first touch, and then sent to the touch control, their response. If it is set to YES, the gesture recognizer will not send the touch to the touched control during the recognition process (note that the recognition process), that is, the control will not have any touch events. The touch event is sent to the touched control only after identification fails. In this case, the response of the control view will be delayed by about 0.15ms. @property BOOL delaysTouchesBegan (nonatomic); / / if touch recognition whether failure to immediately end the touch event @property the gesture recognition (nonatomic) BOOL delaysTouchesEnded; / / assign a gesture to another gesture execution failure can also trigger execution, using multiple gestures one gesture solution sometimes gestures is related. Such as single and double click, click and press down, the moment may only recognize the method can recognize the other click, can specify a gesture, even if they have to meet the conditions, will not immediately trigger, will wait until after the specified gesture determination failed to trigger - (void) requireGestureRecognizerToFail: (UIGestureRecognizer * otherGestureRecognizer); / / get the current touch in the specified view point on the - (CGPoint) lo CationInView: (nullable UIView* view); / / get the hand touch index - (NSUInteger) numberOfTouches; / / multi touch touch point of view relative to the specified location - (CGPoint) locationOfTouch: (NSUInteger) touchIndex inView: (nullable UIView* view); @ end

Two. About the contents of the UIGestureRecognizerDelegate agent

@protocol UIGestureRecognizerDelegate < NSObject> @optional / / call to gesture recognition of the return NO end will no longer trigger gesture recognition, gesture recognition in use: can be used to specify the location of controls - (BOOL) gestureRecognizerShouldBegin: (UIGestureRecognizer * gestureRecognizer); / / support multi gesture trigger, return YES, can be more than a gesture with the trigger method, return NO for mutual exclusion whether to allow multiple gesture recognizers common recognition, a gesture recognition control after whether blocking gesture recognition continues to spread, the default return NO; if YES, the responder chain upper object triggers gesture recognition, if the underlying object is also added to the gesture and successful recognition will continue, otherwise the upper after object recognition will no longer continue to spread - (BOOL) gestureReco Gnizer: (UIGestureRecognizer * gestureRecognizer) shouldRecognizeSimultaneouslyWithGestureRecognizer: (UIGestureRecognizer * otherGestureRecognizer); / / this method returns YES, the first sign and second mutually exclusive, will be the first failure - (BOOL) gestureRecognizer: (UIGestureRecognizer * gestureRecognizer) shouldRequireFailureOfGestureRecognizer: (UIGestureRecognizer *) otherGestureRecognizer NS_AVAILABLE_IOS (7_0); / / this method returns YES, the first and the second exclusive, second will fail (BOOL) (UIGestureRecognizer * gestureRecognizer:) gestureRecognizer shouldBeRequiredToFailByGestureRecognizer: (UIGestureRecognizer *) otherGestureRecognizer NS_AVAILABLE_IOS (7_0); / / finger touch screen after the callback method returns NO, is no longer a gesture recognition, Party By triggering this method in the window object in a touch event occurs before the touchesBegan:withEvent: method calls the gesture recognizer call returns NO if gesture, recognizer will not see the touch event. (by default, YES) - - (BOOL) gestureRecognizer: (UIGestureRecognizer *), gestureRecognizer, shouldReceiveTouch: (UITouch *) touch; @end

Common knowledge:

Whether / / while supporting a variety of gestures - (BOOL) gestureRecognizer: (UIGestureRecognizer * gestureRecognizer) shouldRecognizeSimultaneouslyWithGestureRecognizer: (UIGestureRecognizer *) otherGestureRecognizer{return YES;} / / allows you to click on the start - (BOOL) gestureRecognizerShouldBegin: (UIGestureRecognizer *) gestureRecognizer{return YES;} / / click settings range - (BOOL) gestureRecognizer: (UIGestureRecognizer * gestureRecognizer * UITouch (shouldReceiveTouch:) touch{CGPoint) / / get the touch point CURP = [touch locationInView:self.imageView] current if (curp.x; < return = self.imageView.bounds.size.width*0.5) {NO;}else{return YES;}}

UITapGestureRecognizer and UIButton click event conflict solutions:

- (BOOL) gestureRecognizer: (UIGestureRecognizer * gestureRecognizer) shouldReceiveTouch: (UITouch *) touch{if ([touch.view isKindOfClass:[UIButton class]]) {return NO}; return YES;}

Three. UIGestureRecognizer subclass

UITapGestureRecognizer (touch press) UILongPressGestureRecognizer (press) UISwipeGestureRecognizer (swipe gesture) UIRotationGestureRecognizer (rotation gesture) UIPanGestureRecognizer (drag gesture) UIPinchGestureRecognizer (pinch gesture with zoom)

1. UITapGestureRecognizer (touch, click)

@interface UITapGestureRecognizer: UIGestureRecognizer / / set can recognize gestures at least touch number (default 1) @property NSUInteger numberOfTapsRequired (nonatomic); / / set can identify the number of minimal finger gestures (default 1) @ property (nonatomic) NSUInteger numberOfTouchesRequired; @end


/ / create a gesture UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self object action:@selector (tapAction:)]; / / set can recognize gestures at least touch number tap.numberOfTapsRequired = 3; / / set can identify the gestures at least the finger number tap.numberOfTouchesRequired = 2; / / the gesture object is added to the corresponding controls in [self.imgView addGestureRecognizer:tap];

2:UILongPressGestureRecognizer (long press gesture)

@interface UILongPressGestureRecognizer: UIGestureRecognizer / / set can recognize gestures at least touch number (default 1) @property NSUInteger numberOfTapsRequired (nonatomic); / / set can identify the number of minimal finger gestures (default 1) @property NSUInteger numberOfTouchesRequired (nonatomic); / / set can identify the long press the shortest length gesture at the time, in seconds, the default is 0.5 @property (nonatomic) CFTimeInterval minimumPressDuration; / / the maximum distance, long time setting allows the mobile unit pixel, the default is 10 pixel @property (nonatomic) CGFloat allowableMovement; @end


UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector (longPressAction:)]; / / set can be identified according to the minimum length to long gesture by the time longPress.minimumPressDuration = 0.5; / / "tolerance range" longPress.allowableMovement = 10; / / the long press gesture added to the corresponding controls in [self.imgView addGestureRecognizer:longPress];

3:UISwipeGestureRecognizer (swipe gesture)

Typedef NS_OPTIONS (NSUInteger, UISwipeGestureRecognizerDirection) {UISwipeGestureRecognizerDirectionRight < = 1; < 0, UISwipeGestureRecognizerDirectionLeft = 1 < / right slide; < / / UISwipeGestureRecognizerDirectionUp = 1, the slide to the left 1 < 2, < / / slide up UISwipeGestureRecognizerDirectionDown = 1 < < 3;} / / to decline; @interface UISwipeGestureRecognizer: UIGestureRecognizer / / at least touch the finger number, the default is 1 @property (nonatomic) NSUInteger numberOfTouchesRequired; / / set the swipe gesture support direction, the default is the right and @property (nonatomic) UISwipeGestureRecognizerDirection direction; @end


UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector (swipeAction:)]; swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft; [self.imgView addGestureRecognizer:swipeLeft];

4:UIRotationGestureRecognizer (rotating gesture)

@interface UIRotationGestureRecognizer: UIGestureRecognizer / / rotation angle of @property (nonatomic) CGFloat rotation; / / rotation speed, unit: deg / sec, @property (nonatomic, readonly) CGFloat velocity; @end


/ / picture box to add a UIRotationGestureRecognizer *rotation alloc] rotation gesture = [[UIRotationGestureRecognizer initWithTarget:self action:@selector (rotateAction:)]; rotation.delegate = self; [self.imgView addGestureRecognizer:rotation]; / / monitoring method of rotary gesture - (void) rotateAction: (UIRotationGestureRecognizer * recognizer) {/ / on the basis of the original accumulation, how many degrees recognizer.view.transform = CGAffineTransformRotate (recognizer.view.transform, recognizer.rotation) after the completion of each rotation; / / rotation value, return to the position of 0.Recognizer.rotation = 0;}

5:UIPanGestureRecognizer (drag and drop gesture)

@interface UIPanGestureRecognizer: UIGestureRecognizer / / set the minimum hand trigger drag index, the default is 1 @property (nonatomic) NSUInteger minimumNumberOfTouches; / / set the trigger finger drag up the number of defaults to UINT_MAX infinite @property (nonatomic) NSUInteger maximumNumberOfTouches; / / get the current position of drag - (CGPoint) translationInView: (nullable * UIView) view; / / set the current drag position - (void) setTranslation: (CGPoint) translation inView: (nullable UIView * view); / / set the drag speed, pixels per second - (CGPoint) velocityInView: (nullable * UIView) view; @end


UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector (panAction:)]; [self.imgView addGestureRecognizer:pan]; / / monitoring method of drag gesture - (void) panAction: (UIPanGestureRecognizer * recognizer) {/ / 1. for finger dragging, translation of the value of CGPoint translation = [recognizer translationInView:recognizer.view]; / / the 2. let the current control response (recognizer.view.transform = CGAffineTransformTranslate, recognizer.view.transform translation translation.x, translation.y); / / the 3. time translation gesture recognition is completed, the value of the cumulative [recognizer don't let translation setTranslation:CGPointZero inView:recognizer.view];}

6:UIPinchGestureRecognizer (kneading gestures, scaling)

@interface UIPinchGestureRecognizer: UIGestureRecognizer / zoom @property (nonatomic) CGFloat scale; / / get the kneading speed, unit: zoom / sec @property ratio (nonatomic, readonly) CGFloat velocity; @end


UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector (pinchAction:)]; pinch.delegate = self; [self.imgView addGestureRecognizer:pinch]; / / attached monitoring method - (void) pinchAction: (UIPinchGestureRecognizer * recognizer) {recognizer.view.transform = CGAffineTransformScale (recognizer.view.transform, recognizer.scale, recognizer.scale); recognizer.scale = 1;}

Four. Finally

Send a picture:

UIGestureRecognizer explain
yearns for.Jpg