Do you really know UIGestureRecognizer ?, Uigesturerecognizer

Source: Internet
Author: User

Do you really know UIGestureRecognizer ?, Uigesturerecognizer

I. First, let's take a look at the definition of UIGestureRecognizer.

// The current gesture status typedef NS_ENUM (NSInteger, UIGestureRecognizerState) {// you have not recognized the gesture operation (but may have triggered the touch event). The default status is UIGestureRecognizerStatePossible, // The gesture has started and has been recognized at this time. However, this process may change. The gesture operation has not completed UIGestureRecognizerStateBegan. // The gesture status has changed UIGestureRecognizerStateChanged, // The gesture recognition operation has been completed (at this time, the finger has been released) UIGestureRecognizerStateEnded, // The gesture is canceled, and the UIGestureRecognizerStateCancelled status is restored to the default state, // hand Potential recognition is completed, same as end identifier = identifier}; NS_CLASS_AVAILABLE_IOS (3_2) @ interface UIGestureRecognizer: NSObject // create a gesture object and add the trigger event-(instancetype) initWithTarget :( nullab) target action :( nullable SEL) action NS_DESIGNATED_INITIALIZER; // Add a listening event to a gesture object-(void) addTarget :( id) target action :( SEL) action; // remove a gesture listening event-(void) removeTarget :( nullable id) target action :( nu Llable SEL) action; // obtain the current gesture status @ property (nonatomic, readonly) UIGestureRecognizerState; // delegate @ property (nullable, nonatomic, weak) id <strong> delegate; // identify whether Gesture Recognition is available @ property (nonatomic, getter = isEnabled) BOOL enabled; // obtain the View view read-only @ property (nullable, nonatomic, readonly) UIView * View of gesture touch; // whether to cancel the touch control response is YES by default. In this case, when the gesture reader identifies the touch, it sends a touchesCancelled to the touch control to cancel the touch response of the control view, at this time, only gestures are supported. When it is set to NO, the gesture reader does not send touchesCancelled to the control after recognizing the touch. In this case, both the gesture reader and the control view respond to touch. Note: Gesture Recognition and touch events exist at the same time, only because touchesCancelled leads to failure of touch events and @ property (nonatomic) BOOL cancelsTouchesInView; // whether to delay sending a touch event to the touch control. The default value is NO. In this case, when a touch occurs, the gesture recognition first captures the touch and sends it to the touch control, the two responded to each other. If it is set to YES, the gesture reader does not send the touch to the control during the recognition process (note that it is the recognition process), that is, the control does not have any touch events. The Touch event is sent to the touch control only after the recognition fails. In this case, the response of the control view is delayed by about 0.15 ms. @ Property (nonatomic) BOOL delaysTouchesBegan; // If the touch recognition fails, determines whether to immediately end the touch event of this gesture recognition @ property (nonatomic) BOOL delaysTouchesEnded; // specify a gesture that can be executed only when the other gesture fails to be executed. At the same time, the solution that triggers multiple gestures to use one of the gestures is sometimes associated, such as single-host and double-click, click and long press. When you click Next, you may only recognize that the click cannot recognize others. This method can specify a gesture. Even if you have already met the conditions, it will not be triggered immediately, it will not be triggered until the specified gesture is confirmed to fail-(void) requireGestureRecognizerToFail :( UIGestureRecognizer *) otherGestureRecognizer; // obtain the current vertex (CGPoint) on the specified view) locationInView :( nullable UIView *) view; // obtain the Touch hand index-(NSUInteger) numberOfTouches; // specify the position of the touch point to the specified view-(CGPoint) locationOfTouch :( NSUInteger) touchIndex inView :( nullable UIView *) view; @ end

UIGestureRecognizer is an abstract class that defines the basic behavior of all gestures. You can use its subclass to process specific gestures.

Knowledge Point 1: the sub-classes of UIGestureRecognizer are as follows (these are the classes we usually use directly ):

Instant (touch, TAP) UILongPressGestureRecognizer ← (swipe gesture) UIRotationGestureRecognizer (rotate gesture) UIPanGestureRecognizer Merge (pinch gesture, zoom)

Example:

// Drag the gesture into * pan = [[UIPanGestureRecognizer alloc] initWithTarget: self action: @ selector (pan :)]; [tagButton addGestureRecognizer: pan];-(void) pan :( UIPanGestureRecognizer *) pan {}
// Add the click action UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget: self action: @ selector (tapHandle :)]; [self. view addGestureRecognizer: tap];-(void) tapHandle :( UITapGestureRecognizer *) tap {}

Ii. UIGestureRecognizerDelegate proxy content

@ Protocol UIGestureRecognizerDelegate <NSObject> @ optional // method called when starting Gesture Recognition. If NO is returned, the recognition is stopped and NO gesture is triggered. Its usefulness is as follows: you can use Gesture Recognition (BOOL) gestureRecognizerShouldBegin :( UIGestureRecognizer *) gestureRecognizer; // whether multi-gesture triggering is supported. If YES is returned, multiple gestures can be triggered together, if NO is returned, it indicates whether multiple gesture identifiers are allowed to recognize each other. If NO is returned by default, if YES, after the upper-layer object of the responder chain triggers Gesture Recognition, if the lower-layer object also adds a gesture and successfully identifies it, it will continue to be executed; otherwise, it will not continue propagation after the upper-layer object recognition-(BOOL) gestureRecognizer :( UIGestureRecognizer *) gestureRecogn Izer identifier :( UIGestureRecognizer *) identifier; // This method returns YES. When the first gesture is mutually exclusive with the second one, the first one fails-(BOOL) gestureRecognizer :( UIGestureRecognizer *) gestureRecognizer failed :( optional *) When NS_AVAILABLE_IOS (7_0); // This method returns YES. When the first and second are mutually exclusive, the second will fail-(BOOL) gestureRecognizer :( optional *) gestureRecogn Izer identifier: (UIGestureRecognizer *) otherGestureRecognizer NS_AVAILABLE_IOS (7_0); // Method for background callback on the touch screen. If NO is returned, gesture recognition is not performed, method trigger. This method is called before the touchesBegan: withEvent: Method of gesture recognizer when a touch event occurs in the window object. If NO is returned, gesture recognizer will not see this touch event. (YES by default)-(BOOL) gestureRecognizer :( UIGestureRecognizer *) gestureRecognizer shouldReceiveTouch :( UITouch *) touch; @ end

Knowledge Point 1:

// Whether multiple gestures are supported at the same time-(BOOL) gestureRecognizer :( UIGestureRecognizer *) gestureRecognizer restart :( UIGestureRecognizer *) Restart {return YES;} // whether to allow start clicking) optional :( optional *) gestureRecognizer {return YES;} // set the click range-(BOOL) gestureRecognizer :( optional *) gestureRecognizer shouldReceiveTouch :( UITouch *) touch {// obtain the current touch point CGPoint curp = [touch locationInView: self. imageView]; if (curp. x <= self. imageView. bounds. size. width * 0.5) {return NO;} else {return YES ;}}

Knowledge Point 2: solution to click event conflicts between UITapGestureRecognizer and UIButton

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

Iii. Knowledge about UIGestureRecognizer subclasses

1: UITapGestureRecognizer (touch, click)

NS_CLASS_AVAILABLE_IOS (3_2) @ interface identifier: UIGestureRecognizer // you can specify the minimum number of BITs that can be recognized (1 by default) @ property (nonatomic) NSUInteger numberOfTapsRequired; // set the minimum number of fingers that can recognize a gesture (1 by default) @ property (nonatomic) NSUInteger numberOfTouchesRequired; @ end

Knowledge Point 1: The instance code is as follows:

// Create a gesture object UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget: self action: @ selector (tapAction :)]; // set the minimum number of taps that can recognize a gesture. numberOfTapsRequired = 3; // you can specify the minimum number of fingers that can recognize a gesture. numberOfTouchesRequired = 2; // Add the gesture object to the corresponding control [self. imgView addGestureRecognizer: tap];

2: UILongPressGestureRecognizer (long-pressed gesture)

NS_CLASS_AVAILABLE_IOS (3_2) @ interface identifier: UIGestureRecognizer // you can specify the minimum number of BITs that can be recognized (1 by default) @ property (nonatomic) NSUInteger numberOfTapsRequired; // set the minimum number of fingers that can recognize a gesture (1 by default) @ property (nonatomic) NSUInteger numberOfTouchesRequired; // set the shortest duration that can recognize a long-pressed gesture, unit: seconds. The default value is 0.5 @ property (nonatomic) CFTimeInterval minimumPressDuration. // you can specify the maximum distance allowed to move a long time. Unit: pixels. The default value is 10 pixels. @ property (nonatomic) CGFloat allowableMovement; @ end

Knowledge Point 1: The instance code is as follows:

UILongPressGestureRecognizer * longPress = [[UILongPressGestureRecognizer alloc] initWithTarget: self action: @ selector (longPressAction :)]; // you can specify the minimum long press time that can be recognized by longPress. minimumPressDuration = 0.5; // "fault tolerance range" longPress. allowableMovement = 10; // Add the long-pressed gesture to the corresponding control [self. imgView addGestureRecognizer: longPress];

3: UISwipeGestureRecognizer (swipe gesture)

Typedef NS_OPTIONS (NSUInteger, UISwipeGestureRecognizerDirection, // slide upwards: Finger = 1 <3 // downward}; NS_CLASS_AVAILABLE_IOS (3_2) @ interface UISwipeGestureRecognizer: UIGestureRecognizer // minimum number of touch fingers; default value: 1 @ property) NSUInteger numberOfTouchesRequired; // sets the direction supported by the swipe gesture. The default value is to slide to the right. @ property (nonatomic) UISwipeGestureRecognizerDirection ction; @ end

Knowledge Point 1: The instance code is as follows:

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

4: UIRotationGestureRecognizer (rotation gesture)

NS_CLASS_AVAILABLE_IOS (3_2) @ interface rotation: UIGestureRecognizer // rotation Angle @ property (nonatomic) CGFloat rotation; // rotation speed, unit: Degree/second, @ property (nonatomic, readonly) CGFloat velocity; @ end

Knowledge Point 1: The instance code is as follows:

// Add a rotation gesture UIRotationGestureRecognizer * rotation = [[UIRotationGestureRecognizer alloc] initWithTarget: self action: @ selector (rotateAction :)]; rotation. delegate = self; [self. imgView addGestureRecognizer: rotation]; // method of monitoring rotation gestures-(void) rotateAction :( UIRotationGestureRecognizer *) recognizer {// how many recognizer is accumulated based on the original one. view. transform = CGAffineTransformRotate (recognizer. view. transform, recognizer. rotation); // after each rotation, the rotation value is restored to the zero position. recognizer. rotation = 0 ;}

5: UIPanGestureRecognizer (drag gesture)

NS_CLASS_AVAILABLE_IOS (3_2) @ interface identifier, the default value is UINT_MAX infinite @ property (nonatomic) NSUInteger maximumNumberOfTouches; // obtain the current drag position-(CGPoint) translationInView :( nullable UIView *) view; // set the current drag position-(void) setTranslation :( CGPoint) translation inView :( nullable UIView *) view; // set the drag speed. Unit: pixel/second-(CGPoint) velocityInView :( nullable UIView *) view; @ end

Knowledge Point 1: instance code

UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc] initWithTarget: self action: @ selector (panAction :)]; [self. imgView addGestureRecognizer: pan]; // method of listening to drag and drop gestures-(void) panAction :( UIPanGestureRecognizer *) recognizer {// 1. obtain the translation value CGPoint translation = [recognizer translationInView: recognizer. view]; // 2. let the current control translate the response recognizer. view. transform = CGAffineTransformTranslate (recognizer. view. transform, translation. x, translation. y); // 3. after each translation Gesture Recognition, do not accumulate the translation value [recognizer setTranslation: CGPointZero inView: recognizer. view];}

6: UIPinchGestureRecognizer (pinch gesture, used for scaling)

NS_CLASS_AVAILABLE_IOS (3_2) @ interface Scaling: UIGestureRecognizer // sets the scaling ratio @ property (nonatomic) CGFloat scale; // gets the kneading speed. Unit: Zoom ratio/second @ property (nonatomic, readonly) CGFloat velocity; @ end

Knowledge Point 1: instance code

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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.