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):
从名字上我们就能知道,
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];
- additional setup after loading the view from its nib.
- Uipangesturerecognizer *panrecognizer = [[Uipangesturerecognizer alloc] initwithtarget:self action: @selector ( Handlepanfrom:)];
- [Self.view Addgesturerecognizer:panrecognizer]; //Key statement, add a gesture monitor to Self.view;
- 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 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.