子所不欲,勿施于人.
iOS系统在3.2版本之后提供了一些常用的手势,总共是6大手势,总共是六大类,当然了,当这些手势不能满足我们的工程需要的时候,我们需要自定义手势,这个手势类继承于UIGestureRecognizer这个类.
手势名称 | 手势类 |
---|---|
点击手势 | UITapGestureRecognizer |
轻扫手势 | UISwipeGestureRecognizer |
长按手势 | UILongPressGestureRecognizer |
旋转手势 | UIRotationGestureRecognizer |
拖动手势 | UIPanGestureRecognizer |
捏合手势 | UIPinchGestureRecognizer |
UITapGestureRecognizer (点击手势)
点击手势比较简单,但是也是我们最常用的一种手势,我们只需要创建出手势并且添加到对应的视图上即可.
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];//tapAction:这个方法中我们添加当点击手势触发的时候,我们需要执行的方法
tapGesture.numberOfTapsRequired = 1;
//点击次数
tapGesture.numberOfTouchesRequired = 1;
//点击手指数
[self.view addGestureRecognizer:tapGesture];
UISwipeGestureRecognizer(轻扫手势)
轻扫手势当,有左右轻扫之分,所以我们可以区别对待,向左轻扫和向右轻扫
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
//设置轻扫的方向
swipeGesture.direction = UISwipeGestureRecognizerDirectionRight;
//向右
[self.view addGestureRecognizer:swipeGesture];
UISwipeGestureRecognizer *swipeGestureLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
//设置轻扫的方向
swipeGestureLeft.direction = UISwipeGestureRecognizerDirectionLeft;
//向左
[self.view addGestureRecognizer:swipeGestureLeft];
//轻扫手势触发方法
下面的方法可以进行左右轻扫的设置.
-(void)swipeGesture:(id)sender
{
UISwipeGestureRecognizer *swipe = sender
if(swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
//向左轻扫
} if(swipe.direction == UISwipeGestureRecognizerDirectionRight){
//向右轻扫
}
}
UILongPressGestureRecognizer(长按手势)
长按手势也是一种常用的手势,一般我们会用在图片上面,长按之后出现选项对话框,这时候,用户就可以进行进一步的用户交互了.
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGesture:)];
//设置长按时间
longPressGesture.minimumPressDuration = 0.5;
[self.view addGestureRecognizer:longPressGesture];
//长按手势触发方法
-(void)longPressGesture:(id)sender
{
UILongPressGestureRecognizer *longPress = sender;
if (longPress.state == UIGestureRecognizerStateBegan)
{
}
}
长按手势的常用状态如下
开始:UIGestureRecognizerStateBegan
改变:UIGestureRecognizerStateChanged
结束:UIGestureRecognizerStateEnded
取消:UIGestureRecognizerStateCancelled
失败:UIGestureRecognizerStateFailed
UIPanGestureRecognizer (拖动手势)
拖动手势,也是一种常见的手势,我们一般用图标的拖动.
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];
[self.view addGestureRecognizer:panGesture];
//拖动手势触发方法
-(void) panGesture:(id)sender
{
UIPanGestureRecognizer *panGesture = sender;
CGPoint movePoint = [panGesture translationInView:self.view];
//your code
}
UIPinchGestureRecognizer (捏合手势)
捏合手势一般用于图片放大之后的还原
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGesture:)];
[self.view addGestureRecognizer:pinchGesture];
////捏合手势触发方法
-(void) pinchGesture:(id)sender
{
UIPinchGestureRecognizer *gesture = sender;
//手势改变时
if (gesture.state == UIGestureRecognizerStateChanged)
{
//捏合手势中scale属性记录的缩放比例
_imageView.transform = CGAffineTransformMakeScale(gesture.scale, gesture.scale);
}
//结束后恢复
if(gesture.state==UIGestureRecognizerStateEnded)
{
[UIView animateWithDuration:0.5 animations:^{
_imageView.transform = CGAffineTransformIdentity;//取消一切形变
}];
}
}
UIRotationGestureRecognizer (旋转手势)
旋转手势也是我们常见的一种手势之一
UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGesture:)];
[self.view addGestureRecognizer:rotationGesture];
//旋转手势触发方法
-(void)rotationGesture:(id)sender
{
UIRotationGestureRecognizer *gesture = sender;
if (gesture.state==UIGestureRecognizerStateChanged)
{
_imageView.transform=CGAffineTransformMakeRotation(gesture.rotation);
}
if(gesture.state==UIGestureRecognizerStateEnded)
{
[UIView animateWithDuration:1 animations:^{
_imageView.transform=CGAffineTransformIdentity;//取消形变
}];
}
}
注意:一个手势只能对应一个 View,但是一个 View 可以有多个手势。
原文参考:http://www.open-open.com/lib/view/open1435284827732.html