Detailed Explanation and Example Summary of IOS Gesture Operation

  • 2021-09-11 21:33:29
  • OfStack

Summary of iOS gesture operation

Type of gesture operation

UITapGestureRecognizer: Tapping, clicking UILongPressGestureRecognizer: Long press UIPinchGestureRecognizer: Zoom UIRotationGestureRecognizer: Rotation UISwipeGestureRecongizer: Sweep UIPanGestureRecognizer: Drag

Proxy method for gesture operation (UIGestureRecognizerDelegate)

The condition that the gesture may occur. Returning NO can prevent this gesture from happening or this gesture has no effect


- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer;

Whether to allow multiple gestures to occur at the same time


- (BOOL)gestureRecognizer:(UIGestureRecognizer *)
gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:
(UIGestureRecognizer *)otherGestureRecognizer;

UITapGestureRecognier tapping and clicking gestures

Setting the property numberOfTapsRequired specifies how many fingers are required to trigger an event numberOfTouchesRequired: You can set how many times you want to tap the trigger event

 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];

  //  Set up proxy 
  tap.delegate = self;

  //  Set the number of clicks to trigger gesture events 
  tap.numberOfTapsRequired = 1;

  //  Set the index of hands to be clicked 
  tap.numberOfTouchesRequired = 1;

  [self.image addGestureRecognizer:tap];

UILongPressGestureRecongnizer long press

minimumPressDuration sets the minimum time interval for a long press, that is, the interval between the start of the press and the time when the finger leaves. If it is less than this value, it will not be considered as a long press operation allowableMovement: Allow movement during long press

  UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];

  //  Agent 
  longPress.delegate = self;

  //  Set the minimum interval time ,  Time between finger pressing and leaving 
  longPress.minimumPressDuration = 1.0;

  //  Pixels allowed to move during pressing 
  longPress.allowableMovement = 30;

  [self.image addGestureRecognizer:longPress];

UIPinchGestureRecognizer Zoom Gesture

scale: Set the scaling ratio relative to the original size


 UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];

  //  Agent 
  pinch.delegate = self;

  //  Set the scale 
  pinch.scale = 1.2;

  [self.image addGestureRecognizer:pinch];

UIRotationGestureRecognizer Rotation Gesture

rotation: Rotation radian. To ensure that the rotation starts at the previous position every time, instead of returning to the initial position, this value must be cleared in the action method


- (void)setupRotation
{
  UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];

  //  Set up proxy 
  rotation.delegate = self;

  [self.image addGestureRecognizer:rotation];
}

- (void)rotation:(UIRotationGestureRecognizer *)rotation
{
  //  Angle of rotation 
  CGFloat radian = rotation.rotation;

  self.image.transform = CGAffineTransformRotate(self.image.transform, radian);

  //  Reset, make sure that it is on every time 1 Start turning at the secondary position instead of returning to the initial position and turning again every time 
  rotation.rotation = 0;
}

UISwipeGestureRecognizer gently sweep, press with your finger and slide on the screen

Scanning is divided into four directions (up, down, left and right), and if you want to add more than one swipe action to one control at the same time, you must add one object to each action. That is to say, the action in each direction corresponds to one object.

direction: Specifies the direction of the sweep action


typedef NS_OPTIONS(NSUInteger, UISwipeGestureRecognizerDirection) {
  UISwipeGestureRecognizerDirectionRight = 1 << 0, //  From left to right 
  UISwipeGestureRecognizerDirectionLeft = 1 << 1, //  From right to left 
  UISwipeGestureRecognizerDirectionUp  = 1 << 2, //  From bottom to top 
  UISwipeGestureRecognizerDirectionDown = 1 << 3 //  From top to bottom 
};

 UISwipeGestureRecognizer *swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];

  //  Set up proxy 
  swipeUp.delegate = self;

  //  Modify direction ,  From bottom to top 
  swipeUp.direction = UISwipeGestureRecognizerDirectionUp;

  [self.image addGestureRecognizer:swipeUp];

  //  Add other direction gestures 
  UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];

  //  Modify direction ,  From bottom to top 
  swipeDown.direction = UISwipeGestureRecognizerDirectionDown;

  [self.image addGestureRecognizer:swipeDown];

UIPanGestureRecognizer Drag and Drag Controls

Note: The touch point locationInView of the gesture is different from the moving point translationInView of the gesture. The former is obtained by locationInView, which refers to the coordinates of the finger in the current control, while the latter indicates the rect relative to the parent view


  UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];

  //  Set up proxy 
  pan.delegate = self;

  [self.image addGestureRecognizer:pan];


  //  Touch point of gesture 
  // CGPoint p = [pan locationInView:self.image];

  //  Moving point of gesture ( Displacement point of each movement )
  CGPoint transP = [pan translationInView:self.image];

  NSLog(@"%f, %f", transP.x, transP.y);

  self.image.transform = CGAffineTransformTranslate(self.image.transform, transP.x, transP.y);

  //  Reset 
  [pan setTranslation:CGPointZero inView:self.image];



Thank you for reading, hope to help everyone, thank you for your support to this site!


Related articles: