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):
从名字上我们就能知道,
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
- -(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];
- panrecognizer.maximumnumberoftouches = 1;
- panrecognizer.delegate = self;
-  [PANRECOGNIZER RELEASE];  
- }
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
- -(void) Viewdidload
- {
- //Click the recognizer
- uitapgesturerecognizer* Singlerecognizer;
- Singlerecognizer = [[UITapGestureRecognizer alloc] initwithtarget:selfaction: @selector (Singletap:)];
- //Number of clicks
- singletaprecognizer.numberoftapsrequired = 1; //Click
- //Add a gesture monitor to Self.view;
- [Self.view Addgesturerecognizer:singlerecognizer];
- //Double-click the Recognizer
- uitapgesturerecognizer* Double;
- Doublerecognizer = [[UITapGestureRecognizer alloc] initwithtarget:selfaction: @selector (Doubletap:)];
- doubletaprecognizer.numberoftapsrequired = 2; //double-click
- //Key statement, add a gesture monitor to Self.view;
- [Self.view Addgesturerecognizer:doublerecognizer];
- //Key in this line, double-click gestures to determine the appropriate action to trigger a click gesture when the monitor fails
- [Singlerecognizer Requiregesturerecognizertofail:doublerecognizer];
- [Singlerecognizer release];
- [Doublerecognizer release];
- }
- -(void) Singletap: (uitapgesturerecognizer*) Recognizer
- {
- Handling Click Actions
- }
- -(void) Doubletap: (uitapgesturerecognizer*) Recognizer
- {
- Handling double-click operations
- }
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)