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;
}