iOS Development UI Chapter-ios gesture Recognition (double click, Pinch, rotate, drag, scrub, long press, swipe up or down)

Source: Internet
Author: User

an analysis of the uigesturerecognizer of the gesture operation of iOSI. Overview

The operation of touch screen in iphone, before 3.2 is mainly used by the following 4 ways of Uiresponder :

-(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

But this way of identifying different gestures is really a hassle, and you need to do your own calculations to do different gesture resolution. Later...

Apple has given a more convenient way to use Uigesturerecognizer.

Second, Uigesturerecognizer

UIGestureRecognizer基类 is an abstract class , we mainly use its subclass (name contains link, can click to jump to iOS Developer library, see Official document):

    • UITapGestureRecognizer

    • UIPinchGestureRecognizer

    • UIRotationGestureRecognizer

    • UISwipeGestureRecognizer

    • UIPanGestureRecognizer

    • UILongPressGestureRecognizer

从名字上我们就能知道,Tap (TAP), Pinch (pinch), Rotation (rotate), Swipe (sliding, fast moving, is used to monitor the direction of the slide), Pan (drag, slow movement, is used to monitor the amount of offsets), and longpress (Long Press).

For example, you can add a viewdidload function:

[CPP]View Plaincopy
    1. -(void)  viewdidload  
    2. {  
    3.  [SUPER VIEWDIDLOAD];  
    4.  // do any additional setup after loading the view  from its nib.  
    5.  uipangesturerecognizer * Panrecognizer = [[uipangesturerecognizer alloc] initwithtarget:self action: @selector ( Handlepanfrom:)];  
    6.  [self.view addgesturerecognizer:panrecognizer];
    7.  panrecognizer.maximumnumberoftouches  = 1;  
    8.  panrecognizer.delegate = self;  
    9.  [PANRECOGNIZER RELEASE];  
    10. }  

Other gesture methods are similar.

The core is to set the delegate and use Addgesturerecognizer to add the specified gesture monitoring on the view that requires gesture monitoring.

Of course remember to add <UIGestureRecognizerDelegate> to the header of the view as a delegate.

But some gestures are related, what do we do? For example tap and longpress, swipe and Pan, or tap once with tap twice.

gesture recognition is a principle of mutual exclusion , such as clicking and double-clicking, and if it recognizes a gesture, then the gesture will not be recognized . So for associated gestures, to do special processing to help the program to identify, you should put the current gesture into which type of gesture.

For example, when clicking and double-clicking coexist, it can only send a clicked message if it is not processed. To be able to recognize the double-tap gesture, you need to make a special processing logic, that is, to determine whether the gesture is a double-click, in the case of double-clicking failure as a gesture processing. Use

[a requiregesturerecognizertofail:b] function, which can specify that when a gesture occurs, even if a is satisfied, it will not be triggered immediately and will wait until The specified gesture B is not triggered until it is determined to fail.

[CPP]View Plaincopy
  1. -(void) Viewdidload
  2. {
  3. //Click the recognizer
  4. uitapgesturerecognizer* Singlerecognizer;
  5. Singlerecognizer = [[UITapGestureRecognizer alloc] initwithtarget:selfaction: @selector (Singletap:)];
  6. //Number of clicks
  7. singletaprecognizer.numberoftapsrequired = 1; //Click
  8. //Add a gesture monitor to Self.view;
  9. [Self.view Addgesturerecognizer:singlerecognizer];
  10. //Double-click the Recognizer
  11. uitapgesturerecognizer* Double;
  12. Doublerecognizer = [[UITapGestureRecognizer alloc] initwithtarget:selfaction: @selector (Doubletap:)];
  13. doubletaprecognizer.numberoftapsrequired = 2; //double-click
  14. //Key statement, add a gesture monitor to Self.view;
  15. [Self.view Addgesturerecognizer:doublerecognizer];
  16. //Key in this line, double-click gestures to determine the appropriate action to trigger a click gesture when the monitor fails
  17. [Singlerecognizer Requiregesturerecognizertofail:doublerecognizer];
  18. [Singlerecognizer release];
  19. [Doublerecognizer release];
  20. }
  21. -(void) Singletap: (uitapgesturerecognizer*) Recognizer
  22. {
  23. Handling Click Actions
  24. }
  25. -(void) Doubletap: (uitapgesturerecognizer*) Recognizer
  26. {
  27. Handling double-click operations
  28. }
Iii. approximate types of iphone operating gestures

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

2. Drag (Drag)
Drag to implement scrolling of some pages, as well as the movement of controls.

3. Slide (Flick)
Swipe for the ability to quickly scroll and page through pages.

4. Sweep (Swipe)
Sweep gesture the shortcut menu for activating list items

5. Double click (double tap)
Double-click to zoom in and center the picture, or restore the original size (if it is currently zoomed in). Also, double-click to activate the Edit menu for text.

6. Zoom in (Pinch Open)
Zooming in gestures enables you to open a feed and open the details of the article. Magnification gestures can also be used to enlarge a picture when viewed in a photo.

7. Zoom Out (Pinch close)
To zoom out, you can implement features that are reversed and correspond to the magnification gesture: turn off the feed to exit to the first page, and close the article to the index. When you view a photo, the zoom out gesture also allows you to zoom out of the image.

8. Long press (Touch &hold)
On my Subscriptions page, long press feeds will automatically go into edit mode, while selecting the feeds that your finger is currently pressing. You can then drag the feed to move the location directly.
With the long press on the text, the Magnifier accessibility feature appears. When released, the Edit menu appears.
Long press on the picture, the Edit menu appears.

9. Shaking (Shake)
Shake gesture, the undo and Redo menu will appear. is primarily for user text input.

iOS Development UI Chapter-ios gesture Recognition (double-click, pinch, rotate, drag, scrub, long press, swipe up or down)

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.