标签:
1. UIGestureRecognizer手势识别器
利用UIGestureRecognizer,能轻松识别用户在某个view上面做的一些常见手势
UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,使用它的子类才能处理具体的手势
2. 触摸手指类型
UITapGestureRecognizer(敲击)
UIPinchGestureRecognizer(捏合,用于缩放)
UIPanGestureRecognizer(拖拽)
UISwipeGestureRecognizer(轻扫)
UIRotationGestureRecognizer(旋转)
UILongPressGestureRecognizer(长按)
3.手势使用方法
1> 创建手势并监听
2> 添加手势
3> 实现手势方法
1. 点击/敲击 : UITapGestureRecognizer
1> 创建
- (void)setUpTap
{
// 点击
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
[self.imageView addGestureRecognizer:tap];
tap.delegate = self;
}
2 > 实现方法
- (void)tap:(UITapGestureRecognizer *)tap
{
NSLog(@"%s", __func__);
}
3> 点击手势成为代理<UIGestureRecognizerDelegate>,实现shouldReceiveTouch,可设定某些范围可以点击
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
// 获取当前点的位置
CGPoint curP = [touch locationInView:self.imageView];
if (curP.x > self.imageView.bounds.size.width * 0.5) { // 右边不可接受点击事件
return NO;
} else { // 左边可以接受点击事件
return YES;
}
}
2. 捏合(放大/缩小) : UIPinchGestureRecognizer
1> 对象创建
// 捏合
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
pinch.delegate = self;
[self.imageView addGestureRecognizer:pinch];
- (void)pinch:(UIPinchGestureRecognizer *)pinch
{
// 捏合缩放
self.imageView.transform = CGAffineTransformScale(self.imageView.transform, pinch.scale, pinch.scale);
// 恢复缩放
[pinch setScale:1];
}
3. 旋转 : UIRotationGestureRecognizer
1> 对象创建
// 旋转
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
rotation.delegate = self;
[self.imageView addGestureRecognizer:rotation];
- (void)rotation:(UIRotationGestureRecognizer *)rotation
{
self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, rotation.rotation);
// 旋转清零
[rotation setRotation:0];
}
4. 拖动(移动) : UIPanGestureRecognizer
1> 对象创建
// 拖动
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[self.imageView addGestureRecognizer:pan];
- (void)pan:(UIPanGestureRecognizer *)pan
{
// 计算拖动距离(相对于初始位置)
CGPoint transP = [pan translationInView:self.imageView];
self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, transP.x, transP.y);
// 拖动距离请0
[pan setTranslation:CGPointZero inView:self.imageView];
}
5. 轻扫手势 : UISwipeGestureRecognizer
1> 对象创建
// 滑动 (默认向右滑)
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
// 设置滑动方向
/*
UISwipeGestureRecognizerDirectionRight = 右滑
UISwipeGestureRecognizerDirectionLeft = 左滑
UISwipeGestureRecognizerDirectionUp = 上滑
UISwipeGestureRecognizerDirectionDown = 下滑
*/
swipe.direction = UISwipeGestureRecognizerDirectionUp;
[self.imageView addGestureRecognizer:swipe];
- (void)swipe:(UISwipeGestureRecognizer *)swipe
{
NSLog(@"%s", __func__);
}
6. 长按 : UILongPressGestureRecognizer
1> 对象创建
//1.创建长按手势
UILongPressGestureRecognizer *longP = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longP:)];
//2.添加手势
[self.imageV addGestureRecognizer:longP];
2> 实现监听方法
//当手指长按的时候调用,注意当长按时,手指移动的时候,会持续调用这个方法,当手指离开时也会调用这个方法.
- (void)longP:(UILongPressGestureRecognizer *)longP{
/*
UIGestureRecognizerStatePossible
UIGestureRecognizerStateBegan = 开始长按
UIGestureRecognizerStateChanged = 长按移动
UIGestureRecognizerStateEnded = 长按结束
UIGestureRecognizerStatePossible
UIGestureRecognizerStateCancelled = 取消长按
UIGestureRecognizerStatePossible
UIGestureRecognizerStateFailed
UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded = 长按结束
*/
if (longP.state == UIGestureRecognizerStateBegan) {
NSLog(@"开始长按");
}else if(longP.state == UIGestureRecognizerStateChanged){
NSLog(@"长按时移动");
}else if(longP.state == UIGestureRecognizerStateEnded){
NSLog(@"手指离开");
}
}
7. 旋转和捏合
1> 添加这2个手势,并设置代理
[self setUpRotation];
[self setUpPinch];
2> 实现代理方法允许多手势
// 允许多触摸事件
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
标签:
原文地址:http://www.cnblogs.com/Xfsrn/p/5000336.html