手势一共分为七种,每一个手势的类都是继承于UIGestureRecognizer,我们操作的时候是操作其子类
一个手势只能添加到一个视图上,如果添加到多个视图上,只能对最后添加的那个视图其效果
轻拍手势(点击)
- (void)viewDidLoad{
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
//设置点击次数
tap.numberOfTapsRequired = 2;//快速点击两次才能触发
//设置点击手指个数
tap.numberOfTouchesRquired = 2;//在模拟器上测试时,按住alt键可以唤出第二根手指,模拟器最多只能有两根;
//UIImageView添加手势(需要打开交互userInteractionEnabled)
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake:(50,50,50,50)];
imgView.userInteractionEnabled = YES;
imgView.backgroundColor = [UIColor redColor];
[self.view addSubview:imgView];
[imgView release];
[imgView addGestureRecognizer:tap];
[tap release];
}
//方法实现
- (void)tapAction:(UITapGestureRecognizer *)tap{
//每个手势都自带一个属性 .view,返回的是这个手势所在的视图
tap.view.frame = CGRectMake(0,0,100,100);
}
轻扫手势
- (void)viewDidLoad{
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
//设置轻扫方向 默认方向是向右轻扫,一个轻扫手势只能支持一个方向
swipe.direction = UISwipeGestureRecognizerDirectionDown;
[imgView addGestureRecognizer:swipe];//在轻拍中进行过初始化
[swipe release];
}
- (void)swipeAction:(UISwipeGestureRecognizer *)swipe{
if (swipe.direction == UISwipeGestureRecognizerDirectionDown) {
NSLog(@"轻扫");
}
}
长按手势
- (void)viewDidLoad{
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
//设置长按时间
longPress.minimumPressDuration = 1;
[imgView addGestureRecognizer:longPress];
[longPress release];
}
- (void)longPressAction:(UILongPressGestureRecognizer *)longPress{
if (longPress.state == UIGestureRecognizerStateBegan) {//注1
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"123" preferredStyle:UIAlertControllerStyleActionSheet];
[self presentViewController:alert animated:YES completion:nil];
}
}
平移手势
- (void)viewDidLoad{
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
[imgView addGestureRecognizer:pan];
[pan release];
}
- (void)panAction:(UIPanGestureRecognizer *)pan{
if (pan.state == UIGestureRecognizerStateChanged) {//注1
CGPoint point = [pan translationInView:pan.view];
//transform是视图的一个重要属性
//我们的平移、旋转、缩放,改变的都是视图的transform属性,我们用何种方法去改变它,取决于赋值符号右边调用的API
pan.view.transform = CGAffineTransformTranslate(pan.view.transform,point.x,point.y);
//重新置成0点,每一次平移的结束时的位置会变成下一次平移的起点
[pan setTranslation:CGPointZero inView:pan.view];
}
}
旋转手势
- (void)viewDidLoad{
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
[imgView addGestureRecognizer:rotation];
[rotation release];
}
- (void)rotationAction:(UIRotationGestureRecognizer *)rotation{
if (rotation.state == UIGestureRecognizerStateChanged) {
rotation.view.transform = CGAffineTransformTranslate(rotation.view.transform,rotation.rotation);
//同平移
rotation.rotation = 0;
}
}
缩放手势
- (void)viewDidLoad{
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
[imgView addGestureRecognizer:pinch];
[pinch release];
}
- (void)pinchAction:(UIPinchGestureRecognizer *)pinch{
if (pinch.state == UIGestureRecognizerStateChanged) {
pinch.view.transform = CGAffineTransformScale(pinch.view.transform,pinch.scale,pinch.scale);
//同平移
pinch.scale = 1;
}
}
屏幕边缘平移手势
UIScreenEdgePanGestureRecognizer
平时基本不用;
注:
1.
每一个手势都有一个state属性,表示的是手势识别的不同的阶段
是一个枚举类型
typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {
UIGestureRecognizerStatePossible, //默认状态
UIGestureRecognizerStateBegan,
UIGestureRecognizerStateChanged,
UIGestureRecognizerStateEnded,
UIGestureRecognizerStateCancelled,
UIGestureRecognizerStateFailed,
UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded
};
除了轻拍手势之外,其他的手势都是连续的手势,刚按下去的时候state的状态是Possible,还没有被识别当达到识别条件(如到达longPress的长按时间,或者rotation手势,开始旋转)后会进入状态Began,但这个状态只会保持一瞬,然后进入Changed状态。
所以我们需要按照需求进行判定,如上文的longPress手势会present一个alert出来,如果不进行判定,会多次触发,导致程序出错,所以我们要把判定条件设为Began,这样一次长按就只会触发一次;
注中注:swipe手势只会在�识别结束的时才会触发一次,所以不用进行判定
2.
我们可以看出tap手势是longPress手势的一部分,swipe手势是pan手势的一部分,如果一个视图上多个手势,系统默认就会忽略掉作为其他手势一部分的手势,为了防止这种情况,需要用到*(void)requireGestureRecognizerToFail:(UIGestureRecognizer )otherGestureRecognizer方法
这个方法是由手势调用传入一个手势作为参数,当后一个手势识别失败时才会执行前一个手势
例:
[pan requireGestureRecognizerToFail:swipe];
//只有在swipe手势识别失败后,才会触发pan手势的方法
参考资料
http://www.cnblogs.com/kenshincui/p/3950646.html
崔江涛(KenshinCui)