ios目前支持6中手势识别。
- UITapGestureRecognizer : 点击手势
- UIPinchGestureRecognizer : 捏合手势
- UIPanGestureRecognizer : 拖动手势
- UISwipeGestureRecognizer : 滑动手势
- UIRotationGestureRecognizer : 旋转手势
- UILongPressGestureRecognizer : 长按手势
具体代码执行:
点击手势
1 2 3 4 5 6 7 8 9 10 11
| UITapGestureRecognizer* recognizer; recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapFrom:)]; recognizer.numberOfTapsRequired = 1; // 点击次数 recognizer.numberOfTouchesRequired = 1;//手指个数 [self.view addGestureRecognizer:recognizer]; - (void)handleTapFrom:(UITapGestureRecognizer *)recognizer { recognizer.view.tag//添加手势的视图的tag值 //处理点击事件 }
|
捏合手势
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| //自己设置 imageview和image去 UIImageView *imageview; UIImage *srcImage ; //设置imageview允许用户交互,支持多点触碰 imageview.userInteractionEnabled = YES; imageview.multipleTouchEnabled = YES; UIPinchGestureRecognizer* recognizer; recognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlepinFrom:)]; [imageview addGestureRecognizer:recognizer]; - (void)handlepinFrom:(UIPinchGestureRecognizer *)gesture { CGFloat currentScale; CGFloat scale = gesture.scale; // 如果捏合手势刚刚开始 if (gesture.state == UIGestureRecognizerStateBegan) { // 计算当前缩放比 currentScale = imageview.image.size.width / srcImage.size.width; } // 根据手势处理器的缩放比例计算图片缩放后的目标大小 CGSize targetSize = CGSizeMake(srcImage.size.width * scale * currentScale, srcImage.size.height * scale * currentScale); // 对图片进行缩放 imageview.image = [srcImage imageByScalingToSize:targetSize]; }
|
拖动手势
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| //自己设置 imageview去 UIImageView *imageview; //设置imageview允许用户交互,支持多点触碰 imageview.userInteractionEnabled = YES; imageview.multipleTouchEnabled = YES; UIPanGestureRecognizer* recognizer; recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlepinFrom:)]; //最小手指数 recognizer.minimumNumberOfTouches = 1; //最大手指数 recognizer.maximumNumberOfTouches = 1; [imageview addGestureRecognizer:recognizer]; - (void)handlepinFrom:(UIPanGestureRecognizer *)gesture { //处理坐标 }
|
滑动手势
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| //单独手势一个一个的添加 UISwipeGestureRecognizer *recognizer;
recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)]; [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)]; [self.view addGestureRecognizer:recognizer];
recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)]; [recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)]; [self.view addGestureRecognizer:recognizer];
recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)]; [recognizer setDirection:(UISwipeGestureRecognizerDirectionUp)]; [self.view addGestureRecognizer:recognizer]; recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)]; [recognizer setDirection:(UISwipeGestureRecognizerDirectionDown)]; [[self.view addGestureRecognizer:recognizer]; -(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer{ if(recognizer.direction==UISwipeGestureRecognizerDirectionDown) { NSLog(@"swipe down"); //执行程序 } if(recognizer.direction==UISwipeGestureRecognizerDirectionUp) { NSLog(@"swipe up"); //执行程序 } if(recognizer.direction==UISwipeGestureRecognizerDirectionLeft) { NSLog(@"swipe left"); //执行程序 } if(recognizer.direction==UISwipeGestureRecognizerDirectionRight) { NSLog(@"swipe right"); //执行程序 } }
|
旋转手势
1 2 3 4 5 6 7 8 9 10 11 12
| UIImageView *imageview; UIRotationGestureRecognizer *rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotateView:)]; [imageview addGestureRecognizer:rotationGestureRecognizer];
- (void) rotateView:(UIRotationGestureRecognizer *)rotationGestureRecognizer { UIView *view = rotationGestureRecognizer.view; if (rotationGestureRecognizer.state == UIGestureRecognizerStateBegan || rotationGestureRecognizer.state == UIGestureRecognizerStateChanged) { view.transform = CGAffineTransformRotate(view.transform, rotationGestureRecognizer.rotation); [rotationGestureRecognizer setRotation:0]; } }
|
长按手势
1 2 3 4 5 6 7 8
| UIImageView *imageview; UILongPressGestureRecognizer * longPressGr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressToDo:)]; longPressGr.minimumPressDuration = 1.0;//最短时间 [imageview addGestureRecognizer:longPressGr]; - (void) longPressToDo:(UILongPressGestureRecognizer *)gnizer { //处理长按事件 }
|