关于iOS中的手势识别
2016-07-03 22:45
357 查看
1> UITapGestureRecognizer(敲击,轻按)
2> UIPinchGestureRecognizer(捏合,用于缩放)
3> UIPanGestureRecognizer(拖拽)
4> UISwipeGestureRecognizer(轻扫)
5> UIRotationGestureRecognizer(旋转)
6> UILongPressGestureRecognizer(长按)
一共上面六种
敲击手势:
代码,首先创建一个红色的View(供下面显示用)
// 创建一个红色的View
UIView *redView = [[UIView alloc] init];
redView.frame = self.view.bounds;
redView.backgroundColor = [UIColor redColor];
[self.view addSubview:redView];
self.redView = redView;
创建轻按手势:
- (void)createTapGesture
{
// 创建一个手势,敲击手势
// gesture : 手势
// recoginze : 认出,认识,(在这里翻译成识别)
// 敲击手势识别
// tap : 轻拍
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(tap)];
// 设置手指的根数(也就是需要多少根手指接触屏幕才可以触发事件,默认是一根)
tapGesture.numberOfTouchesRequired = 2;
// 设置敲击的次数,(此处为连续敲击才可以触发这个事件,默认为1)
tapGesture.numberOfTapsRequired = 2;
// 添加手势
[self.redView addGestureRecognizer:tapGesture];
}
// 监听
- (void)tap
{
NSLog(@"%s",__func__);
}
其他基本类似,当需要两个手势同时触发的时候,就要用到手势的代理了:
在此以捏合手势和旋转手势演示
- (void)viewDidLoad {
[superviewDidLoad];
// 创建一个红色的View
UIView *redView = [[UIView alloc] init];
redView.frame = self.view.bounds;
redView.backgroundColor = [UIColorredColor];
[self.view addSubview:redView];
self.redView = redView;
[selfcreatePinchGesture];
[selfcreateRotationGesture];
}
- (void)createPinchGesture
{
// 捏合手势
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizeralloc]initWithTarget:selfaction:@selector(pinch:)];
[self.redView addGestureRecognizer:pinchGesture];
pinchGesture.delegate = self; // 提示:遵守协议
}
- (void)createRotationGesture
{
// 旋转手势
UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizeralloc]initWithTarget:selfaction:@selector(rotation:)];
[self.redView addGestureRecognizer:rotationGesture];
rotationGesture.delegate = self;
}
- (void)pinch:(UIPinchGestureRecognizer *)pinch
{
// 这种会不断地记录原来放大或者缩小的scale,并且在原来的基础上类乘
// 缩放的比例不断增加
self.redView.transform = CGAffineTransformScale(self.redView.transform, pinch.scale, pinch.scale);
NSLog(@"%f",pinch.scale);
// 每次缩放完,还原成原来的值
pinch.scale = 1;
// 这种缩放都是相对与原来的大小
//self.redView.transform = CGAffineTransformMakeScale(pinch.scale, pinch.scale);
//NSLog(@"%s",__func__);
}
- (void)rotation:(UIRotationGestureRecognizer *)rotation
{
// 这种方法会不断记录当前的旋转角度,但是会在当前的旋转角度上不断类乘
self.redView.transform = CGAffineTransformRotate(self.redView.transform, rotation.rotation);
// 还原原来的值
rotation.rotation = 0;
// 下面这种方法记录不了当前的旋转角度,一旦手指松开,重新旋转时,会从原始位置旋转
// 不是我们想要的效果
//self.redView.transform = CGAffineTransformMakeRotation(rotation.rotation);
}
#pragma mark - gestureRecognizer delegate methods
// called when the recognition of one of gestureRecognizer or otherGestureRecognizer would be blocked by the other
// return YES to allow both to recognize simultaneously. the default implementation returns NO (by default no two gestures can be recognized simultaneously)
// 返回YES允许两个手势同时识别,默认返回的NO(默认两个手势不能同时识别)
// simultaneously : 同时地
// prevent : 阻止
// note: returning YES is guaranteed to allow simultaneous recognition. returning NO is not guaranteed to prevent simultaneous recognition, as the other gesture's delegate may return YES
// 提示:返回yes是保证允许同时进行识别
// 返回NO是不保证阻止同时识别,因为其他的手势代理方法可能返回的是YES
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer*)otherGestureRecognizer
{
returnYES;
}
2> UIPinchGestureRecognizer(捏合,用于缩放)
3> UIPanGestureRecognizer(拖拽)
4> UISwipeGestureRecognizer(轻扫)
5> UIRotationGestureRecognizer(旋转)
6> UILongPressGestureRecognizer(长按)
一共上面六种
敲击手势:
代码,首先创建一个红色的View(供下面显示用)
// 创建一个红色的View
UIView *redView = [[UIView alloc] init];
redView.frame = self.view.bounds;
redView.backgroundColor = [UIColor redColor];
[self.view addSubview:redView];
self.redView = redView;
创建轻按手势:
- (void)createTapGesture
{
// 创建一个手势,敲击手势
// gesture : 手势
// recoginze : 认出,认识,(在这里翻译成识别)
// 敲击手势识别
// tap : 轻拍
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(tap)];
// 设置手指的根数(也就是需要多少根手指接触屏幕才可以触发事件,默认是一根)
tapGesture.numberOfTouchesRequired = 2;
// 设置敲击的次数,(此处为连续敲击才可以触发这个事件,默认为1)
tapGesture.numberOfTapsRequired = 2;
// 添加手势
[self.redView addGestureRecognizer:tapGesture];
}
// 监听
- (void)tap
{
NSLog(@"%s",__func__);
}
其他基本类似,当需要两个手势同时触发的时候,就要用到手势的代理了:
在此以捏合手势和旋转手势演示
- (void)viewDidLoad {
[superviewDidLoad];
// 创建一个红色的View
UIView *redView = [[UIView alloc] init];
redView.frame = self.view.bounds;
redView.backgroundColor = [UIColorredColor];
[self.view addSubview:redView];
self.redView = redView;
[selfcreatePinchGesture];
[selfcreateRotationGesture];
}
- (void)createPinchGesture
{
// 捏合手势
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizeralloc]initWithTarget:selfaction:@selector(pinch:)];
[self.redView addGestureRecognizer:pinchGesture];
pinchGesture.delegate = self; // 提示:遵守协议
}
- (void)createRotationGesture
{
// 旋转手势
UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizeralloc]initWithTarget:selfaction:@selector(rotation:)];
[self.redView addGestureRecognizer:rotationGesture];
rotationGesture.delegate = self;
}
- (void)pinch:(UIPinchGestureRecognizer *)pinch
{
// 这种会不断地记录原来放大或者缩小的scale,并且在原来的基础上类乘
// 缩放的比例不断增加
self.redView.transform = CGAffineTransformScale(self.redView.transform, pinch.scale, pinch.scale);
NSLog(@"%f",pinch.scale);
// 每次缩放完,还原成原来的值
pinch.scale = 1;
// 这种缩放都是相对与原来的大小
//self.redView.transform = CGAffineTransformMakeScale(pinch.scale, pinch.scale);
//NSLog(@"%s",__func__);
}
- (void)rotation:(UIRotationGestureRecognizer *)rotation
{
// 这种方法会不断记录当前的旋转角度,但是会在当前的旋转角度上不断类乘
self.redView.transform = CGAffineTransformRotate(self.redView.transform, rotation.rotation);
// 还原原来的值
rotation.rotation = 0;
// 下面这种方法记录不了当前的旋转角度,一旦手指松开,重新旋转时,会从原始位置旋转
// 不是我们想要的效果
//self.redView.transform = CGAffineTransformMakeRotation(rotation.rotation);
}
#pragma mark - gestureRecognizer delegate methods
// called when the recognition of one of gestureRecognizer or otherGestureRecognizer would be blocked by the other
// return YES to allow both to recognize simultaneously. the default implementation returns NO (by default no two gestures can be recognized simultaneously)
// 返回YES允许两个手势同时识别,默认返回的NO(默认两个手势不能同时识别)
// simultaneously : 同时地
// prevent : 阻止
// note: returning YES is guaranteed to allow simultaneous recognition. returning NO is not guaranteed to prevent simultaneous recognition, as the other gesture's delegate may return YES
// 提示:返回yes是保证允许同时进行识别
// 返回NO是不保证阻止同时识别,因为其他的手势代理方法可能返回的是YES
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer*)otherGestureRecognizer
{
returnYES;
}
相关文章推荐
- Swift中实现点击、双击、捏、旋转、拖动、划动、长按手势的类和方法介绍
- 基于Android中手势交互的实现方法
- Android程序开发之给背景图加上移动的手势
- 实例详解用户输入 i. 检测常用手势
- Android基础开发之手势识别
- Android应用开发中触摸屏手势识别的实现方法解析
- Android实现手势控制ImageView图片大小
- 基于JS实现Android,iOS一个手势动画效果
- 使用Swift代码实现iOS手势解锁、指纹解锁实例详解
- IOS手势操作(拖动、捏合、旋转、点按、长按、轻扫、自定义)
- iOS开发中的几个手势操作实例分享
- Android手势操作示例(上/下/左/右的判断)
- Android手势密码的实现
- iOS开发之触摸事件以及手势
- HTML5单页面手势滑屏切换原理
- iOS 使用手势UIGestureRecognizer对图像进行缩放、移动、旋转等操作
- iOS缩放手势
- 高仿QQ、微信用户图像处理
- iOS的事件
- iOS- UIImageView/手势识别