Uigesturerecognizer
The Uigesturerecognizer class is used to detect and identify gestures used by users when they use the device. It is an abstract class that defines the basic behavior of all gestures. The following is a Uigesturerecognizer subclass that handles specific user gesture behavior:
UITapGestureRecognizer//1. Click
Uilongpressgesturerecognizer//3. Long Press
Uiswipegesturerecognizer//4. Swipe
Uipangesturerecognizer//5. Mobile
Uirotationgesturerecognizer//6. Rotate
Uipinchgesturerecognizer//7. Kneading
To create a gesture:
// 1. Click
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
[imgView addGestureRecognizer:tap];
// 2. Double click
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapAction:)];
doubleTap.numberOfTapsRequired = 2;
[imgView addGestureRecognizer:doubleTap];
// Double click to fail to click
[tap requireGestureRecognizerToFail:doubleTap];
// 3. Long press
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
/ / set the minimum time
longPress.minimumPressDuration = 1;
[imgView addGestureRecognizer:longPress];
// 4. Swipe
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
// set the swipe direction
[swipe setDirection:UISwipeGestureRecognizerDirectionRight];
[imgView addGestureRecognizer:swipe];
// 5. Move
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
[imgView addGestureRecognizer:pan];
// swipe to move before moving
[pan requireGestureRecognizerToFail:swipe];
// 6. Rotate
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
[imgView addGestureRecognizer:rotation];
// 7. Kneading
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
[imgView addGestureRecognizer:pinch];
Gesture Trigger Event:
Gestureaction:
-(void)longPressAction:(UILongPressGestureRecognizer *)longPress
{
If (longPress.state == UIGestureRecognizerStateBegan) {
NSLog (@" long press start");
}else if (longPress.state == UIGestureRecognizerStateEnded){
NSLog (@" long press end");
}
}
- (void)panAction:(UIPanGestureRecognizer *)pan {
//The coordinates of the finger
CGPoint point = [pan locationInView:self.view];
_view.center = point;
}
- (void)rotationAction:(UIRotationGestureRecognizer *)rotation
{
If (rotation.state == UIGestureRecognizerStateChanged) {
/ / Get the arc
CGFloat angle = rotation.rotation;
//Rotating
Rotation.view.transform = CGAffineTransformMakeRotation(angle);
} else if (rotation.state == UIGestureRecognizerStateEnded) {
//reduction
[UIView animateWithDuration:.5 animations:^{
Rotation.view.transform = CGAffineTransformIdentity;
}];
}
}
- (void)pinchAction:(UIPinchGestureRecognizer *)pinch
{
If (pinch.state == UIGestureRecognizerStateChanged) {
// Get the zoom ratio
CGFloat scale = pinch.scale;
// zoom
Pinch.view.transform = CGAffineTransformMakeScale(scale, scale);
} else if (pinch.state == UIGestureRecognizerStateEnded) {
[UIView animateWithDuration:.5 animations:^{
Pinch.view.transform = CGAffineTransformIdentity;
}];
}
}
Motion Shake gesture
/ / Let the current object become the first responder
- (BOOL)canBecomeFirstResponder
{
Return YES;
}
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
NSLog (@"shakes a start");
}
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
NSLog (@"shakes the end");
}
Recommend a detailed use of iOS gesture Recognition article: iOS gesture recognition
iOS gesture recognizer