我们在开发过程中为了提高用户体验,以及实现更好的用户交互往往会使用UiGestureRecognizer(手势识别器)这一类
其实UiGestureRecognizer这一类并不复杂,简单来说这就是一个特殊的触摸事件,UiGestureRecognizer是一个父类,而实际操作中我们要使用它的子类,较为常用的有以下几种:
UITapGestureRecognizer | 轻拍手势 |
UISwipeGestureRecognizer | 轻扫手势 |
UILongPressGestureRecognizer | 长按手势 |
UIPanGestureRecognizer | 平移手势 |
UIPinchGestureRecognizer | 捏合(缩放)手势 |
UIRotationGestureRecognizer | 旋转手势 |
UIScreenEdgePanGestureRecognizer | 屏幕边缘平移 |
UITapGestureRecognizer(轻拍手势)
手势的用法大同小异,我们以UITapGestureRecognizer(轻拍手势)为例
首先我们在viewDidLoad中建立一个测试View
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
_testView = [[UIView alloc]initWithFrame:CGRectMake(50, 50, 314, 500)];
_testView.backgroundColor = [UIColor orangeColor];
[self.view addSubview:_testView];
}
效果如图:
接下来我们需要在viewDidLoad方法中给这个view添加手势
//创建手势 使用initWithTarget:action:的方法创建
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapView:)];
//设置属性
//tap 手势一共两个属性,一个是设置轻拍次数,一个是设置点击手指个数
//设置轻拍次数
tap.numberOfTapsRequired = 2;
//设置手指字数
tap.numberOfTouchesRequired = 2;
//别忘了添加到testView上
[_testView addGestureRecognizer:tap];
实现触发事件,此处触发的事件是改变testView的颜色
#pragma mark --- UITapGestureRecognizer 轻拍手势事件 ---
-(void)tapView:(UITapGestureRecognizer *)sender{
//设置轻拍事件改变testView的颜色
_testView.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];
}
实现的效果如下
UISwipeGestureRecognizer (轻扫手势)
我们仍然使用testView这一场景
//创建手势
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeView:)];
//设置属性,swipe也是有两种属性设置手指个数及轻扫方向
swipe.numberOfTouchesRequired = 2;
//设置轻扫方向(默认是从左往右)
//direction是一个枚举值有四个选项,我们可以设置从左往右,从右往左,从下往上以及从上往下
//设置轻扫方向(默认是从左往右)
swipe.direction = UISwipeGestureRecognizerDirectionLeft;
// [_redView addGestureRecognizer:swipe];
触发的事件为:
#pragma mark swipe轻扫手势事件
-(void)swipeView:(UISwipeGestureRecognizer *)sender{
_testView.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];
}
UILongPressGestureRecognizer(长按手势)
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
//属性设置
//最小长按时间
longPress.minimumPressDuration = 2;
[_testView addGestureRecognizer:longPress];
触发事件:
#pragma mark langPress 长按手势事件
-(void)longPress:(UILongPressGestureRecognizer *)sender{
//进行判断,在什么时候触发事件
if (sender.state == UIGestureRecognizerStateBegan) {
NSLog(@"长按状态");
//改变testView颜色
_testView.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];
}
}
UIPanGestureRecognizer(平移手势)
创建手势
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panView:)];
[_testView addGestureRecognizer:pan];
触发事件
#pragma mark pan 平移手势事件
-(void)panView:(UIPanGestureRecognizer *)sender{
CGPoint point = [sender translationInView:_testView];
// sender.view.transform = CGAffineTransformMake(1, 0, 0, 1, point.x, point.y);
//平移一共两种移动方式
//第一种移动方法:每次移动都是从原来的位置移动
// sender.view.transform = CGAffineTransformMakeTranslation(point.x, point.y);
//第二种移动方式:以上次的位置为标准(移动方式 第二次移动加上第一次移动量)
sender.view.transform = CGAffineTransformTranslate(sender.view.transform, point.x, point.y);
//增量置为o
[sender setTranslation:CGPointZero inView:sender.view];
_testView.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];
}
UIScreenEdgePanGestureRecognizer(屏幕边缘平移手势)
创建手势
UIScreenEdgePanGestureRecognizer *screenEdgePan = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(screenEdgePanView:)];
//注意:,使用屏幕边界平移手势,需要注意两点
//1. 视图位置(屏幕边缘)
//2. 设置edges属性
// 设置屏幕边缘手势支持方法
screenEdgePan.edges = UIRectEdgeLeft;
//属性设置
[_testView addGestureRecognizer:screenEdgePan];
触发事件
#pragma mark screenEdgePan 屏幕边界平移事件
-(void)screenEdgePanView:(UIScreenEdgePanGestureRecognizer *)sender{
//计算偏移量
CGPoint point = [sender translationInView:_testView];
//进行平移
sender.view.transform = CGAffineTransformMakeTranslation(point.x, point.y);
}
UIPinchGestureRecognizer(捏合手势)
创建手势
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchView:)];
[_testView addGestureRecognizer:pinch];
触发事件
#pragma mark pinch 捏合手势事件
-(void)pinchView:(UIPinchGestureRecognizer *)sender{
//scale 缩放比例
// sender.view.transform = CGAffineTransformMake(sender.scale, 0, 0, sender.scale, 0, 0);
//每次缩放以原来位置为标准
// sender.view.transform = CGAffineTransformMakeScale(sender.scale, sender.scale);
//每次缩放以上一次为标准
sender.view.transform = CGAffineTransformScale(sender.view.transform, sender.scale, sender.scale);
//重新设置缩放比例 1是正常缩放.小于1时是缩小(无论何种操作都是缩小),大于1时是放大(无论何种操作都是放大)
sender.scale = 1;
}
UIRotationGestureRecognizer(旋转手势)
创建手势
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationView:)];
[_testView addGestureRecognizer:rotation];
触发事件
-(void)rotationView:(UIRotationGestureRecognizer *)sender{
// sender.view.transform = CGAffineTransformMake(cos(M_PI_4), sin(M_PI_4), -sin(M_PI_4), cos(M_PI_4), 0, 0);
//捏合手势两种改变方式
//以原来的位置为标准
// sender.view.transform = CGAffineTransformMakeRotation(sender.rotation);//rotation 是旋转角度
//两个参数,以上位置为标准
sender.view.transform = CGAffineTransformRotate(sender.view.transform, sender.rotation);
//消除增量
sender.rotation = 0.0;
}
合理的运用手势会使我们的app拥有更好的体验,但是在使用手势的同时也要注意避免手势和手势间,以及手势和其他控件间的冲突。