IOS. UI advanced. UITouch touch with UIGesture gesture. 03. gesture reader (UIGestureRecognizer), uigesture.03

Source: Internet
Author: User

IOS. UI advanced. UITouch touch with UIGesture gesture. 03. gesture reader (UIGestureRecognizer), uigesture.03

1. What is a gesture reader:

The UIGestureRecognizer is used to identify whether a user uses a device, such as a gesture, a slide, a tap, or something else.

UIGestureRecognizer class, used to detect and recognize gestures used by users when using devices. It is an abstract class that defines the basic behavior of all gestures. The following is a subclass of UIGestureRecognizer.

Specific user gestures:

  • UITapGestureRecognizer (click)
  • UIPinchGestureRecognizer (kneading)
  • UIPanGestureRecognizer)
  • UISwipeGestureRecognizer (sweep)
  • UIRotationGestureRecognizer (rotate)
  • UILongPressGestureRecognizer (long press)

They all inherit from UIGestureRecognizer and are its subclasses.

Ii. Instances

Take UITapGestureRecognizer as an example:

Let's first look at its two attributes:

@ Property (nonatomic) NSUInteger numberOfTapsRequired; // Default is 1. The number of taps required to match

@ Property (nonatomic) NSUInteger numberOfTouchesRequired; // Default is 1. The number of fingers required to match

The number of times the numberOfTapsRequired attribute can be tapped. The default value is one. The numberOfTouchesRequired attribute allows you to set a few finger clicks. The default value is one finger. The two attributes can be used together to achieve different effects.

Sample Code:

-(Void) viewDidLoad {

[Super viewDidLoad];

// Add a gesture gesture1 and implement the method gestureAction

UITapGestureRecognizer * gesture1 = [[UITapGestureRecognizer alloc] initWithTarget: self action: @ selector (sigleGestureAction)];

[Self. view addGestureRecognizer: gesture]; // do not forget to add a gesture. Here, the gesture is added to self. view.

// Add a double-click gesture to achieve double-click.

UITapGestureRecognizer * gesture2 = [[UITapGestureRecognizer alloc] initWithTarget: self action: @ selector (doubelGestureAction)];

Gesture2.numberOfTapsRequired = 2; // if you double-click it, set this attribute value to OH 2.

[Self. view addGestureRecognizer: gesture2];

Gesture1 requireGestureRecognizerToFail: gesture2]; // note that this code separates the two gestures.

}

-(Void) sigleGestureAction {

NSLog (@ "click ");

}

-(Void) doubelGestureAction {

NSLog (@ "double-click ");

}

The methods for adding other gestures are basically the same. When you add an object, you can check the document, set the attributes, and write down the added method.

 

  

    

For iOS beginners, if any errors occur, please kindly advise.

 

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.