iOS various gestures, very interesting

Source: Internet
Author: User

I. 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 W Ithevent: (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. additional setup after loading the view from its nib.
    5. Uipangesturerecognizer *panrecognizer = [[Uipangesturerecognizer alloc] initwithtarget:self action: @selector ( Handlepanfrom:)];
    6. [Self.view Addgesturerecognizer:panrecognizer]; //Key statement, add a gesture monitor to Self.view;
    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 to use as the most commonly used gesture for pressing or selecting a control or entry (similar to a normal mouse click),

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

3. Slide (Flick) slide is used to realize the fast scrolling and paging of the page.

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

5. Double click (double tap) to enlarge 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. Magnification (Pinch open) magnification gestures enable the following functions: Open the 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 gesture, you can achieve the function that is opposite to the magnifying gesture and corresponding function: Turn off the feed to exit to the homepage, close the article exit to the index page. 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 subscription page, long press feeds will automatically go into edit mode, and select the feed 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. Shake (Shake) shake gesture, the undo and Redo menu will appear. is primarily for user text input.

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.