敲击手势
// 创建一个UILabel用来做测试(可以根据实际需要自己替换)
UILabel *demoLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
// 创建一个单击手势
// 参数1.谁响应这个手势
// 参数2.为手势点击添加一个方法
UITapGestureRecognizer *tapFs = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapFs:)];
// 将手势添加到视图上
[demoLabel addGestureRecognizer:tapFs];
// 设置敲击次数
tapFs.numberOfTapsRequired = 1; //单击
// 设置需要几根手指
tapFs.numberOfTouchesRequired = 1;
// 创建一个双击手势
UITapGestureRecognizer *tapSe = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapSe:)];
// 将手势添加到视图上
[demoLabel addGestureRecognizer:tapSe];
// 设置敲击次数
tapSe.numberOfTapsRequired = 2;
// 让单击手势仅在双击手势不成立时执行
[tapFs requireGestureRecognizerToFail:tapSe];
-(void)tapGesture:(UITapGestureRecognizer *)tap{
NSLog(@"单击了demoLabel");
}
-(void)twoTapGesture:(UITapGestureRecognizer *)tap{
NSLog(@"双击了demoLabel");
}
长按手势
// 创建长按手势
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
// 将手势添加到视图上
[demoLabel addGestureRecognizer:longPress];
// 设置最小的触发时间
longPress.minimumPressDuration = 3;//默认0.5s
// 允许在长按时移动的最大范围,出范围,手势失效
longPress.allowableMovement = 50;
-(void)longPress:(UILongPressGestureRecognizer *)longPress{
//【注意】如果不做任何处理,长按手势添加的事件,在手势结束时再 次被触发
//解决:对手势的状态进行判断,如果手势即将结束响应功能
if (longPress.state == UIGestureRecognizerStateEnded) {
NSLog(@"长按了demoLabel");
}
}
滑动手势
// 创建滑动手势
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
[demoLabel addGestureRecognizer:swipe];
/**滑动方向
UISwipeGestureRecognizerDirectionRight
UISwipeGestureRecognizerDirectionLeft
UISwipeGestureRecognizerDirectionUp
UISwipeGestureRecognizerDirectionDown
*/
// 注意:一个手势只能监听一个方向
swipe.direction = UISwipeGestureRecognizerDirectionRight;
-(void)swipe:(UISwipeGestureRecognizer *)sw{
switch (sw.direction) {
case UISwipeGestureRecognizerDirectionRight:
NSLog(@"向右滑动");
break;
case UISwipeGestureRecognizerDirectionLeft:
NSLog(@"向左滑动");
break;
case UISwipeGestureRecognizerDirectionUp:
NSLog(@"向上滑动");
break;
case UISwipeGestureRecognizerDirectionDown:
NSLog(@"向下滑动");
break;
default:
NSLog(@"没有响应");
break;
}
}
拖动手势
// 创建拖动手势
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];
[demoLabel addGestureRecognizer:pan];
// 设置手指的范围(max最大,min最小)
pan.maximumNumberOfTouches = 3;
pan.minimumNumberOfTouches = 1;
//在拖动过程的每一时刻,都会调用这个方法
-(void)panGesture:(UIPanGestureRecognizer *)pan{
//被拖动的视图相对于父视图原位置的位移
CGPoint point = [pan translationInView:self.view];
//设置新的中心位置
demoLabel.center = CGPointMake(demoLabel.center.x + point.x, demoLabel.center.y + point.y);
//重置被拖动的视图相对与父视图的位置
[pan setTranslation:CGPointZero inView:self.view];
}
旋转手势
//创建旋转手势
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGesture:)];
[demoLabel addGestureRecognizer:rotation];
//只要旋转就会调用该方法
-(void)rotationGesture:(UIRotationGestureRecognizer *)rotation{
demoLabel.transform = CGAffineTransformRotate(demoLabel.transform, rotation.rotation);
//重置旋转角度
rotation.rotation = 0;
}
捏合手势
// 创建捏合手势
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGesture:)];
// 将手势添加到视图上
[demoLabel addGestureRecognizer:pinch];
//只要发生捏合就会调用
-(void)pinchGesture:(UIPinchGestureRecognizer *)pinch{
demoLabel.transform = CGAffineTransformScale(_label.transform, pinch.scale, pinch.scale);
//重置比例
pinch.scale = 1;
}
两种手势作用在同一个视图
//两种手势同时作用在一个视图上
//创建旋转手势
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGesture:)];
rotation.delegate = self;
[demoLabel addGestureRecognizer:rotation];
//创建捏合手势
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGesture:)];
pinch.delegate = self;
//将手势添加到视图上
[demoLabel addGestureRecognizer:pinch];
//只要旋转就会调用该方法
-(void)rotationGesture:(UIRotationGestureRecognizer *)rotation{
demoLabel.transform = CGAffineTransformRotate(demoLabel.transform, rotation.rotation);
//重置旋转角度
rotation.rotation = 0;
}
//只要发生捏合就会调用
-(void)pinchGesture:(UIPinchGestureRecognizer *)pinch{
demoLabel.transform = CGAffineTransformScale(demoLabel.transform, pinch.scale, pinch.scale);
//重置比例
pinch.scale = 1;
}
//如果同一个控件同时添加多个手势,每个手势都需要设置代理,原因是当手势冲突,可以再代理中判断当前是哪一个手势除法 避免冲突
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
//当添加多个手势发现只能实现其中一个方法是,需要重写该代理方法,将返回值设为YES
return YES;
}