There are six types of gesture recognition in iOS development:
Tap gestures (Tapgesturerecognizer),
Swipe gesture (Swipegesturerecognizer),
Long-press gesture (Longpressgesturerecognizer),
Drag gestures (Pangesturerecognizer),
Pinch gesture (Pinchgesturerecognizer),
Rotation gesture (Rotationgesturerecognizer),
1, tap gesture (Tapgesturerecognizer)
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];tapGesture.numberOfTapsRequired = 1; //点击次数tapGesture.numberOfTouchesRequired = 1; //点击手指数[self.view addGestureRecognizer:tapGesture];//轻击手势触发方法-(void)tapGesture:(UITapGestureRecognizer *)sender{ //your code}
2, long press gesture (Longpressgesturerecognizer)
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGesture:)];//设置长按时间longPressGesture.minimumPressDuration = 0.5;[self.view addGestureRecognizer:longPressGesture];//长按手势触发方法-(void)longPressGesture:(id)sender{ UILongPressGestureRecognizer *longPress = sender; if (longPress.state == UIGestureRecognizerStateBegan) { //your code }}说明:长按手势的常用状态如下开始:UIGestureRecognizerStateBegan改变:UIGestureRecognizerStateChanged结束:UIGestureRecognizerStateEnded取消:UIGestureRecognizerStateCancelled失败:UIGestureRecognizerStateFailed
3, swipe gesture (Swipegesturerecognizer)
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];//设置轻扫的方向swipeGesture.direction = UISwipeGestureRecognizerDirectionRight; //向右[self.view addGestureRecognizer:swipeGesture];UISwipeGestureRecognizer *swipeGestureLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];//设置轻扫的方向swipeGestureLeft.direction = UISwipeGestureRecognizerDirectionLeft; //向左[self.view addGestureRecognizer:swipeGestureLeft];//轻扫手势触发方法-(void)swipeGesture:(id)sender{ UISwipeGestureRecognizer *swipe = sender; if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) { //向左轻扫 } if (swipe.direction == UISwipeGestureRecognizerDirectionRight) { //向右轻扫 }}
4, pinch gesture (pinchgesturerecognizer)
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGesture:)];[self.view addGestureRecognizer:pinchGesture]; ////捏合手势触发方法-(void) pinchGesture:(id)sender{ UIPinchGestureRecognizer *gesture = sender; //手势改变时 if (gesture.state == UIGestureRecognizerStateChanged) { //捏合手势中scale属性记录的缩放比例 _imageView.transform = CGAffineTransformMakeScale(gesture.scale, gesture.scale); } //结束后恢复 if(gesture.state==UIGestureRecognizerStateEnded) { [UIView animateWithDuration:0.5 animations:^{ _imageView.transform = CGAffineTransformIdentity;//取消一切形变 }]; }}
5, drag gesture (Pangesturerecognizer)
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];[self.view addGestureRecognizer:panGesture];//拖动手势触发方法-(void) panGesture:(id)sender{ UIPanGestureRecognizer *panGesture = sender; CGPoint movePoint = [panGesture translationInView:self.view]; //your code}
6, rotation gesture (Rotationgesturerecognizer)
UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGesture:)];[self.view addGestureRecognizer:rotationGesture];//旋转手势触发方法-(void)rotationGesture:(id)sender{ UIRotationGestureRecognizer *gesture = sender; if (gesture.state==UIGestureRecognizerStateChanged) { _imageView.transform=CGAffineTransformMakeRotation(gesture.rotation); } if(gesture.state==UIGestureRecognizerStateEnded) { [UIView animateWithDuration:1 animations:^{ _imageView.transform=CGAffineTransformIdentity;//取消形变 }]; }}
For more iOS development-related technologies, please follow iOS development public number iOS:
iOSDevTip
Posted by Li gang Jan-th , 10:40 am iOS development
The source of this article just online: http://www.superqq.com/blog/2015/01/14/ioskai-fa-zhi-shou-shi-shi-bie-hui-zong/
Summary of gesture recognition for iOS development