SoFunction
Updated on 2025-04-07

IOS  Detailed explanation of gesture operations and example summary

iOS gesture operation summary

Types of gesture operations

  • UITapGestureRecognizer: tap, click
  • UILongPressGestureRecognizer: Long press
  • UIPinchGestureRecognizer: Zoom
  • UIRotationGestureRecognizer: Rotate
  • UISwipeGestureRecongizer: Swipe
  • UIPanGestureRecognizer: drag and drop

Proxy method for gesture operation (UIGestureRecognizerDelegate)

Conditions where gestures may occur, returning NO can prevent this gesture from happening or this gesture does not produce any effect.

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

Whether multiple gestures are allowed to occur simultaneously

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

UITapGestureRecognier tap, click gesture

  • Set the property numberOfTapsRequired to specify how many fingers are required to trigger the event
  • numberOfTouchesRequired: You can set the trigger event that needs to be tapped several times.
 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];

  // Set up a proxy   = self;

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

  // Set the number of fingers to click   = 1;

  [ addGestureRecognizer:tap];

UILongPressGestureRecongnizer long press

  • minimumPressDuration sets the minimum interval for long presses, that is, the interval between the start and the fingers leave. If it is less than this value, it will not be considered a long press operation
  • allowableMovement: Whether to allow movement during long pressing
  UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];

  // acting   = self;

  // Set the minimum interval time, the interval between finger pressing and leaving   = 1.0;

  // The pixels allowed to move during pressing   = 30;

  [ addGestureRecognizer:longPress];

UIPinchGestureRecognizer zoom gesture

scale: Set the scale relative to the original size

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

  // acting   = self;

  // Set the scaling ratio   = 1.2;

  [ addGestureRecognizer:pinch];

UIRotationGestureRecognizer Rotating Gesture

rotation: To rotate the radian, to ensure that the rotation starts at the last 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 a proxy   = self;

  [ addGestureRecognizer:rotation];
}

- (void)rotation:(UIRotationGestureRecognizer *)rotation
{
  // Rotation angle  CGFloat radian = ;

   = CGAffineTransformRotate(, radian);

  // Reset to ensure that the rotation starts at the last position every time, instead of returning to the initial position and then turning every time   = 0;
}

UISwipeGestureRecognizer swipe, press your finger and swipe on the screen

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

direction: Specify the direction of the swipe 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 a proxy   = self;

  // Modify the direction, from bottom to top   = UISwipeGestureRecognizerDirectionUp;

  [ addGestureRecognizer:swipeUp];

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

  // Modify the direction, from bottom to top   = UISwipeGestureRecognizerDirectionDown;

  [ addGestureRecognizer:swipeDown];

UIPanGestureRecognizer drag and press the drag control operation

Note: The touch point of the gesture locationInView and the moving point of the gesture translationInView are different. The former uses the locationInView to obtain the coordinates of the finger in the current control, and the latter represents the rect relative to the parent view.

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

  // Set up a proxy   = self;

  [ addGestureRecognizer:pan];

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

  // The movement point of the gesture (the displacement point of each movement)  CGPoint transP = [pan translationInView:];

  NSLog(@"%f, %f", , );

   = CGAffineTransformTranslate(, , );

  // Reset  [pan setTranslation:CGPointZero inView:];



Thank you for reading, I hope it can help you. Thank you for your support for this site!