UIGestureRecognizer手势
目录
1.什么是手势以及手势的种类
2.手势的添加及常用属性
3.多种手势并用
1.什么是手势以及手势的种类
UIKit中包含了UIGestureRecognizer类,用于检测发生在设备中的手势。UIGestureRecognizer基类是一个抽象类,定义了所有手势的基本行为,我们主要是使用它的子类,它有下面一些子类用于处理具体的手势:
1、UITapGestureRecognizer 轻击手势(点一下)
2、UIPinchGestureRecognizer 捏合手势(二指往內或往外拨动,用于缩放)
3、UIPanGestureRecognizer 拖动手势(摇动或者拖拽)
4、UISwipeGestureRecognizer 轻扫手势(擦碰)
5、UIRotationGestureRecognizer 旋转手势(旋转,手指朝相反方向移动)
6、UILongPressGestureRecognizer 长按手势(长按)
对于不同类型的手势识别器,具有不同的配置属性。比如UITapGestureRecognizer,可以配置拍击次数。界面接收到手势之后,可以发送一个消息,用于处理响应手势动作后的任务。当然,不同的手势识别器,发送的消息方法也会有所不同。
2.手势的添加及常用属性
添加手势时,一般是给某一个试图添加一个手势,例如给UIView,UIImageView添加手势,使之实现轻击、旋转、捏合、拖动、长按、滑动等事件。下面以给UIImageView添加手势为例说明6种手势的使用。
- 1.UITapGestureRecognizer 轻击手势
代码:
// 创建一个轻击手势对象,当轻击事件发生,就会调用self里的tap:这个方法
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
// 设置点击需要的手指头的个数!
tap.numberOfTapsRequired = 2;
//将手势添加到图片视图上
[_imageView addGestureRecognizer:tap];
// 双击时执行的方法:双击放大
- (void)tap:(UITapGestureRecognizer *)tap {
[UIView animateWithDuration:0.5 animations:^{
_imageView.transform = CGAffineTransformScale(_imageView.transform, 1.1, 1.1);
}];
}
代码说明
(1) 创建轻击手势使用UITapGestureRecognizer的alloc + initWithTarget:(id) action:(SEL)方法,当手势发生的时候,就会调用target对象的action方法。
(2) 手势的numberOfTouchesRequired属性意思是点击需要的手指头的个数,这个属性类型是NSUInteger,不设置的情况下默认是1,单击时就会触发事件,设置成2时,则点击两次才会触发target对象的action事件。
(3) 获取当前的图片视图_imageView,使用这个视图的addGestureRecognizer:添加手势tap,这样就可以在轻击的时候实现target对象的action方法了。
- 2.UIPinchGestureRecognizer 捏合手势
代码:
// 创建一个捏合手势
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
pinch.delegate = self;
[_imageView addGestureRecognizer:pinch];
- (void)pinch:(UIPinchGestureRecognizer *)pinch {
// pinch.scale 比例, pinch.velocity 强度 比例/s
_imageView.transform = CGAffineTransformScale(_imageView.transform, pinch.scale, pinch.scale);
// 重新设置比例为1.
pinch.scale = 1;
}
代码说明
(1) 创建捏合手势使用UIPinchGestureRecognizer的alloc + initWithTarget:(id) action:(SEL)方法,当手势发生的时候,就会调用target对象的action方法。
(2) 同样使用视图的addGestureRecognizer:添加手势捏合,捏合手势多用于放大或缩小图片视图。
- 3.UIPanGestureRecognizer 拖动手势
代码:
// 创建一个拖动手势
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(dealPan:)];
//将手势添加到图片视图上
[imageView addGestureRecognizer:pan];
//消息方法dealPan:
-(void)dealPan:(UIPanGestureRecognizer *)pan
{
//获取拖移的位置
//屏幕上位置
CGPoint screenPoint = [pan locationInView:self.view];
imageView.center = screenPoint;
}
代码说明
(1) 使用UIPanGestureRecognizer的alloc + initWithTarget:(id) action:(SEL)方法创建拖动手势,当手势发生的时候,就会调用target对象的action方法。
(2) 同样使用视图的addGestureRecognizer:方法给图片视图添加拖动手势,当拖动视图时,就会调用-(void)dealPan:(UIPanGestureRecognizer *)pan这个方法,手势的locationInView:方法会获取手势在视图上移动的点,再将这个点设置为图片视图的中心点,图片视图就会随着手指移动的方向而移动。
- 4.UISwipeGestureRecognizer 轻扫手势
代码:
// 创建一个轻扫手势,当发生时,就调用self的swipe:方法
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
// 设置轻扫的方向, 枚举 (上下左右) ,注意,这个枚举值不要或在一起用
swipe.direction = UISwipeGestureRecognizerDirectionRight;
[_imageView addGestureRecognizer:swipe];
//消息方法
// 往右扫透明度降低
- (void)swipe:(UISwipeGestureRecognizer *)swipe {
if (_imageView.alpha > 0.1) {
_imageView.alpha -= 0.1;
}
}
代码说明
(1) 使用UISwipeGestureRecognizer的alloc + initWithTarget:(id) action:(SEL)方法创建清扫手势,当手势发生的时候,就会调用target对象的action方法。
(2) 设置清扫手势的direction属性,意思是清扫的方向,这个属性类型是UISwipeGestureRecognizerDirection,是一个枚举,值都有UISwipeGestureRecognizerDirectionRight,UISwipeGestureRecognizerDirectionLeft,
UISwipeGestureRecognizerDirectionUp,
UISwipeGestureRecognizerDirectionDown,
分别设置不同清扫方向下的action方法。(3) 同样使用视图的addGestureRecognizer:方法给图片视图添加清扫手势,当按照一定的方向清扫图片视图时,就会调用- (void)swipe:(UISwipeGestureRecognizer *)swipe这个方法,每清扫一次图片视图的透明度就会降低0.1。
- 5.UIRotationGestureRecognizer 旋转手势
代码:
// 创建一个旋转手势,当发生时,就调用self的rotation:方法
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
// 将旋转手势的代理,设置为rootViewController
rotation.delegate = self;
//添加手势
[_imageView addGestureRecognizer:rotation];
//消息方法rotation:
- (void)rotation:(UIRotationGestureRecognizer *)rotation {
// NSLog(@"%.2f", rotation.rotation);
// 旋转弧度,在已经旋转的基础上,继续旋转。
// rotation.velocity 旋转的强度 rotation/s
_imageView.transform = CGAffineTransformRotate(_imageView.transform, rotation.rotation);
// 将旋转之后的角度清0!
rotation.rotation = 0;
}
代码说明
(1) 使用UIRotationGestureRecognizer的alloc + initWithTarget:(id) action:(SEL)方法创建旋转手势。
(2) 同样使用视图的addGestureRecognizer:方法给图片视图添加旋转手势,当用手指旋转视图时,就会调用- (void)rotation:(UIRotationGestureRecognizer *)rotation这个方法,从而使图片发生旋转。
- 6.UILongPressGestureRecognizer 长按手势
代码:
// 创建一个长按手势
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
// 最小的持续时间
longPress.minimumPressDuration = 2.0;
// 允许移动的最大范围
longPress.allowableMovement = 5.0;
// 图片视图上添加手势
[_imageView addGestureRecognizer:longPress];
// 长按手势,会触发两次,一次是手势发生了,一次是抬起
- (void)longPress:(UILongPressGestureRecognizer *)longPress {
// 判断状态,只在手势发生的时候执行if里的语句
if (longPress.state == UIGestureRecognizerStateBegan) {
UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"哎呦" message:@"哎呦" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[av show];
}
}
代码说明
(1) 使用UILongPressGestureRecognizer的alloc + initWithTarget:(id) action:(SEL)方法创建长按手势。
(2) 长按手势的minimumPressDuration属性意思是最小的持续时间,只有长按的时间大于这个最小持续时间才会判定手势为长按手势,这个属性类型是CFTimeInterval,需要传入一个时间间隔,不设置的情况下默认是0.5,即长按时间只要超过0.5秒就会触发action方法。
(3) 长按手势的allowableMovement属性意思是允许移动的最大范围,这个属性类型是CGFloat,需要传入一个CGFloat对象,不设置的情况下默认是10,即长按手势移动的距离超过10,action方法就不会被触发。
(4) 同样使用视图的addGestureRecognizer:方法给图片视图添加长按手势,当长按视图时,就会调用- (void)longPress:(UILongPressGestureRecognizer *)longPress这个方法,弹出一个警告框UIAlertView。但是这个方法会被调用两次,一次是手势发生的时候,一次是手抬起的时候。可以利用长按手势的state属性来判断手势的状态是否是UIGestureRecognizerStateBegan,即手势开始发生的时候,从而使长按手势事件只会被调用一次。
3.多种手势并用
// 创建一个轻击手势对象,当轻击事件发生,就会调用self里的tap:这个方法
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
// 设置点击需要的手指头的个数!
tap.numberOfTapsRequired = 2;
//将手势添加到图片视图上
[_imageView addGestureRecognizer:tap];
// 对于视图控件,可以添加多个手势 (也不宜太多)
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap:)];
[_imageView addGestureRecognizer:singleTap];
// singleTap 这个手势响应,要在tap手势响应失败后,才触发。
[singleTap requireGestureRecognizerToFail:tap];
// 双击时执行的方法:双击放大
- (void)tap:(UITapGestureRecognizer *)tap {
[UIView animateWithDuration:0.5 animations:^{
_imageView.transform = CGAffineTransformScale(_imageView.transform, 1.1, 1.1);
}];
}
// 单击时执行的方法:单击缩小
- (void)singleTap:(UITapGestureRecognizer *)tap {
[UIView animateWithDuration:0.5 animations:^{
_imageView.transform = CGAffineTransformScale(_imageView.transform, 0.9, 0.9);
}];
}