Analysis of UIGestureRecognizer in Gesture Operation of ios (Recommended)

  • 2021-08-12 03:48:04
  • OfStack

1. Overview

Before 3.2, the operation of touch screen in iPhone mainly used the following four ways from 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

However, it is really troublesome to identify different gestures in this way, and you need to calculate and distinguish different gestures by yourself. Later. . .

Apple has given a relatively simple way, that is, to use UIGestureRecognizer.

2. UIGestureRecognizer

The UIGestureRecognizer base class is an abstract class, and we mainly use its subclasses (the name contains links, you can click to skip to iOS Developer library and see the official documents):

UITapGestureRecognizer UIPinchGestureRecognizer UIRotationGestureRecognizer UISwipeGestureRecognizer UIPanGestureRecognizer UILongPressGestureRecognizer

From the names, we can know that Tap (click), Pinch (knead), Rotation (rotate), Swipe (slide, fast move, which is used to monitor the direction of slide), Pan (drag, slow move, which is used to monitor the amount of offset) and LongPress (long press).

For example, you can add 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, give self.view Add 1 Gesture monitoring;  
 panRecognizer.maximumNumberOfTouches = 1; 
 panRecognizer.delegate = self; 
 [panRecognizer release]; 
} 

Other gestures are similar.

Its core is to set delegate and use addGestureRecognizer to add specified gesture monitoring on view that needs gesture monitoring.

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

But some gestures are related. What should I do? For example, Tap and LongPress, Swipe and Pan, or Tap once and Tap times.

Gesture recognition has the principle of mutual exclusion, such as click and double click. If it recognizes one gesture, the subsequent gestures will not be recognized. Therefore, for related gestures, special treatment should be done to help the program identify, and the current gestures should be attributed to which type of gestures.

For example, when clicking and double-clicking coexist, it can only send the clicked message if it is not processed. In order to recognize the double-click gesture, it is necessary to do a special processing logic, that is, to judge whether the gesture is double-click first, and to treat it as a click gesture when the double-click fails. Use
[A requireGestureRecognizerToFail: B] function, which specifies that when an A gesture occurs, even if A is sufficient, it will not be triggered immediately, but will not be triggered until the specified gesture B fails.


- (void)viewDidLoad  
{ 
  //  Clicked  Recognizer 
  UITapGestureRecognizer* singleRecognizer; 
  singleRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:selfaction:@selector(SingleTap : )]; 
  // Number of clicks  
  singleTapRecognizer.numberOfTapsRequired = 1; //  Click  
 
  // To self.view Add 1 Gesture monitoring;  
 
 [self.view addGestureRecognizer:singleRecognizer]; 
 
  
  //  Double-clicked  Recognizer 
  UITapGestureRecognizer* doubleRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:selfaction:@selector(DoubleTap : )]; 
  doubleRecognizer.numberOfTapsRequired = 2; //  Double-click  
  // Key statement, give self.view Add 1 Gesture monitoring;  
  [self.view addGestureRecognizer:doubleRecognizer]; 
   
  //  The key is here 1 Line, the double-click gesture determines that the monitoring failure will trigger the corresponding operation of the click gesture  
  [singleRecognizer requireGestureRecognizerToFail:doubleRecognizer]; 
  [singleRecognizer release]; 
  [doubleRecognizer release]; 
} 
 
-(void)SingleTap:(UITapGestureRecognizer*)recognizer 
{ 
// Handle click action  
} 
 
-(void)DoubleTap:(UITapGestureRecognizer*)recognizer 
{ 
// Handle double-click operations  
} 

3. Approximate types of iphone gestures

1. Click (Tap)

Click is the most commonly used gesture for pressing or selecting a control or item (similar to a normal mouse click),

2. Drag (Drag)

Drag is used to scroll 1 pages and move controls.

3. Slide (Flick)

Sliding is used to realize the function of fast scrolling and turning pages.

4. Sweep (Swipe)

The sweep gesture is used to activate the shortcut menu of list items

5. Double-click (Double Tap)

Double-click Zoom in and center the picture, or restore it to its original size (if it is currently enlarged). At the same time, double-click to activate the text editing menu.

6. Zoom in (Pinch open)

The zoom-in gesture can achieve the following functions: open the feed and open the details of the article. When viewing photos, the zoom-in gesture can also realize the function of zoom-in pictures.

7. Reduce (Pinch close)

Zoom gesture can achieve the opposite and corresponding functions of zoom gesture: close the feed and exit to the home page, close the article and exit to the index page. When viewing photos, the zoom gesture can also realize the function of zoom pictures.

8. Long press (Touch) & Hold)

On the My Subscriptions page, a long press on a feed automatically enters edit mode, while selecting the feed currently pressed by your finger. At this time, you can directly drag the feed to move the location.

Press long for text, and magnifying glass accessibility will appear. When released, the Edit menu appears.

Press long for the picture, and the edit menu will appear.

9. Shake (Shake)

Shake the gesture, and the Undo and Redo menus will appear. Mainly for user text input.


Related articles: