手势识别器通用代码注释如下(以tap为例):
//0.打开UIImageView的用户交互(UIImageView默认不能进行用户交互)self.iconView.userInteractionEnabled = YES;//1.创建手势识别器UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];//1.1.设置手势识别器的属性//设置用户必须点击几次才能触发点击事件tap.numberOfTapsRequired = 2;//设置用户必须几根手指同时点击才会触发事件tap.numberOfTouchesRequired = 2;tap.delegate = self;(设置代理并遵守代理协议)//2.添加手势识别器到view[self.iconView addGestureRecognizer:tap];//3.监听手势识别器[tap addTarget:self action:@selector(tapView)]; //该方法返回一个BOOL值,该BOOL值决定了是否响应此次点击事件。所以如果返回NO,就不会调用tapView方法。- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizershouldReceiveTouch:(UITouch *)touch {//获取点击的位置CGPoint point = [touch locationInView:touch.view];return (point.x > self.iconView.frame.size.width * 0.5)?YES :NO; //监听手势的方法-(void)tapView{ NSLog(@“图片被点击了”);}
具体应用:
一、敲击
1.添加一个UIImageView属性iconView
2.敲击事件-(void)test{self.iconView.userInteractionEnabled = YES;UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init]; tap.numberOfTapsRequired = 2;tap.numberOfTouchesRequired = 2;[self.iconView addGestureRecognizer:tap];[tap addTarget:self action:@selector(tapView)];}-(void)tapView{ NSLog(@"敲击事件");}
二、长按+轻扫
1.添加一个UIView属性costomView2.长按事件-(void)test{UILongPressGestureRecognizer longPress = [[UILongPressGestureRecognizer alloc] init];//1.1设置长按手势识别器的属性longPress.minimumPressDuration = 5;//手指按下后事件响应之前允许手指移动的偏移位longPress.allowableMovement = 10;[self.customView addGestureRecognizer:longPress];[longPress addTarget:self action:@selector(longPressView)];}-(void)longPressView{ NSLog(@“长按事件”);}
3.轻扫事件
-(void)test{UISwipeGestureRecognizer Swipe = [[UISwipeGestureRecognizer alloc] init];//设置轻扫的方向(每行代码代表一个方向,总共4个方向,可以共存)swipe.direction = UISwipeGestureRecognizerDirecionDown;[self.customView addGestureRecognizer:Swipe];[Swipe addTarget:self action:@selector(SwipeView)];}-(void)SwipeView{ NSLog(@“轻扫事件”);}
三、旋转+放缩
1.添加一个UIImageView属性iconView2.旋转(记得在故事板右边开启多点触控)-(void)test{ UIRotationGestureRecognizer *gesture = [[UIRotationGestureRecognizer alloc] init]; [self.iconView addGestureRecognizer:gesture]; gesture.delegate = self; [gesture addTarget:self action:@selector(rotationView:)];}-(void)rotationView:(UIRotationGestureRecognizer *)gesture{ NSLog(@"旋转事件 %.1f", gesture.rotation); //在传入的transform基础上递增一个弧度 self.iconView.transform = CGAffineTransformMakeRotate (self.inconView.transform,gesture.rotation); //每次都把上一次手势的位置作为0度,不这样做的话,如果本来是45度,下次再转1度就会在原有基础上增加46度,而这样就不对了。 gestrue.rotation =0;}
3.放缩
-(void)test{ //捏合手势 UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] init]; [self.iconView addGestureRecognizer:pinch]; pinch.delegate = self; [pinch addTarget:self action:@selector(pinchView)];}-(void)pinchView:(UIPinchGestureRecognizer *)pinch{ self.iconView.transform = CGAffineTransformScale(self.iconView.transform,pinch.scale,pinch.scale); //如果上一次的位置不设置为1.0,就会在原有基础上继续放缩,这是不对的。 pinch.scale = 1.0;}
4.遵从代理协议,实现代理方法,使其支持多手势同时进行
//该方法返回的BOOL值决定了view是否能够同时响应多个手势-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultanenouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{ return YES;}
四、拖拽
1.添加一个UIImageView属性costomView2.拖拽-(void)test{UIPanGestureRecognizer *pan= [[UIPanGestureRecognizer alloc] init];[self.costomView addGestureRecognizer:pan];[pan addTarget:self action:@selector(panView:)];}- (void)panView{ //返回的值是以手指按下的点为原点 CGPoint point = [pan translationInView:pan.view]; CGPoint temp = self.customView.center; temp.x += point.x; temp.y += point.y; self.customView.center = temp; //将其清零 [pan.setTranlation:CGPointZero inView:pan.view];}