Analysis of uigesturerecognizer in IOS gesture operations

Source: Internet
Author: User
I. Overview

For iPhone touch screen processing, before 3.2UiresponderThe following four methods are provided:

-(Void) touchesbegan :( nsset *) touches withevent :( uievent *) event
-(Void) touchescancelled :( nsset *) touches withevent :( uievent *) event
-(Void) touchesended :( nsset *) touches withevent :( uievent *) event
-(Void) touchesmoved :( nsset *) touches withevent :( uievent *) event

However, it is difficult to identify different gesture operations in this way. You need to calculate different gesture resolutions by yourself. Later...

Apple provides a simple way to useUigesturerecognizer.

Ii. uigesturerecognizer

Uigesturerecognizer base classIs an abstract class, We mainlyUse its subclass(The name contains the link. You can click to jump to the IOS Developer Library to view the official documentation ):

  • UITapGestureRecognizer

  • UIPinchGestureRecognizer

  • UIRotationGestureRecognizer

  • UISwipeGestureRecognizer

  • UIPanGestureRecognizer

  • UILongPressGestureRecognizer

We can know from the name,Tap, pinch, rotation, swipe, and pan, is used to monitor the amount of offset) and longpress (long press ).

For example, you can add the following in the viewdidload function:

-(Void) viewdidload {[Super viewdidload]; // do any additional setup after loading the view from Its nib. uipangesturerecognizer * panrecognizer = [[uipangesturerecognizer alloc] initwithtarget: Self action: @ selector (handlepanfrom :)]; [self. view addgesturerecognizer: panrecognizer]; // key statement, to self. view to add a gesture monitoring; panrecognizer. maximumnumberoftouches = 1; panrecognizer. delegate = self; [panrecognizer release];}

Other gestures are similar.

Its core is to set the delegate and use addgesturerecognizer on the view for gesture monitoring to add the specified gesture monitoring.

Of course, remember to add <uigesturerecognizerdelegate> to the view header file of the delegate.

But some gestures are associated. What should I do? For example, tap and longpress, swipe and pan, or tap twice.

Gesture Recognition is mutually exclusive.,For example, if you click or double-click a gesture to recognize it, the subsequent gesture will not be recognized.. Therefore, for associated gestures, special processing is required to help programs identify the types of gestures that should be attributed to the current gestures.

For example, if you click and double-click to coexist, if you do not process it, it can only send the clicked message. To recognize double-click gestures, you need to make a special processing logic, that is, first determine whether the gesture is double-click. When the double-click fails, it is processed as a click gesture. Use

[A requiregesturerecognizertofail: B]Function, which specifies that when a gesture occurs, even if a has reached the limit,It will not be triggered immediately.,Will waitThe specified gesture B is triggered only after confirmation of failure.

-(Void) viewdidload {// click the recognizer handler * singlerecognizer; singlerecognizer = [[externalloc] initwithtarget: selfaction: @ selector (singletap :)]; // The number of clicks. numberoftapsrequired = 1; // click // to self. view to add a gesture monitoring; [self. view progress: singlerecognizer]; // double-click recognizer progress * double; doublerecognizer = [[initalloc] initwithtarget: selfaction: @ selector (doubletap :)]; doubletaprecognizer. numberoftapsrequired = 2; // double-click the key statement and give it to self. view to add a gesture monitoring; [self. view addgesturerecognizer: doublerecognizer]; // This line is critical. If you double-click a gesture to confirm that the monitoring fails, the corresponding operation of the click gesture will be triggered. [singlerecognizer failed: doublerecognizer release]; [doublerecognizer release];}-(void) singletap :( uitapgesturerecognizer *) recognizer {// processing click operation}-(void) doubletap :( uitapgesturerecognizer *) recognizer {// processing double-click operation}
Iii. Approximate types of iPhone operation gestures

1. Click (TAP)
Click is the most commonly used gesture, used to press or select a control or entry (similar to a common mouse click ),

2. Drag (drag)
Drag is used to scroll some pages and move controls.

3. Flick)
Slide enables quick Page scrolling and page turning.

4. Sweep (swipe)
The sweep gesture is used to activate the shortcut menu of the list items.

5. Double-click (double tap)
Double-click to zoom in and show the image in the center, or restore the original size (if the current size has been enlarged ). You can also double-click to activate the text editing menu.

6. Zoom in (pinch open)
The zoom-In gesture enables the following functions: Open the subscription source and open the details of the article. When you view a photo, you can also use the zoom-In gesture to zoom in the image.

7. Zoom out (pinch close)
Zoom-out gesture: disables the subscription source and exits to the home page, and closes the article to the index page. When you view a photo, the zoom-In gesture can also be used to zoom out the image.

8. Long press (touch & hold)
On the my subscription page, the long-pressed subscription source will automatically enter the edit mode and select the subscription source currently pressed by the finger. In this case, you can directly drag the position of the subscription source to move.
If you press the text in a long way, the magnifier function is displayed. The Edit menu is displayed.
The Edit menu is displayed when you press and hold the image.

9. Shake (shake)
The undo and redo menus are displayed with the shaking gesture. It is mainly for user text input.

You are welcome to join the QQ Group for discussion with iPhone developers who like to communicate and be enthusiastic about it. The verification code is 186739796 and the verification code is csdn.

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.