UIGestureRecognizer类,用于检测发生在设备中的手势。
UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,它有下面一些子类用于处理具体的手势:
1、拍击 UITapGestureRecognizer (任意次数的拍击)
通过设置属性" numberOfTapsRequired "实现单击,或双击
2、向里或向外捏 UIPinchGestureRecognizer (用于缩放)
通过设置属性" scale "实现对UI控件大小进行缩放的改变,如:
float lastScale = 0.0;
UILabel *view = (UILabel *)recognizer.view;
recognizer.scale = recognizer.scale - lastScale + 1.0;
view.transform = CGAffineTransformScale(view.transform, recognizer.scale, recognizer.scale);
lastScale = recognizer.scale;
3、摇动或者拖拽 UIPanGestureRecognizer
4、滑动 UISwipeGestureRecognizer (以任意方向)
通过设置属性" direction "实现滑动方向
5、旋转 UIRotationGestureRecognizer (手指朝相反方向移动)
通过设置属性" rotation "实现对UI控件旋转的改变,如:
UILabel *view = (UILabel *)recognizer.view;
float rotationV = recognizer.rotation;
view.transform = CGAffineTransformMakeRotation(rotationV);
6、长按 UILongPressGestureRecognizer
通过设置属性" longpressRecognizer "实现长按响应时间
对于不同类型的手势识别器,具有不同的配置属性。
比如UITapGestureRecognizer,可以配置拍击次数。界面接收到手势之后,可以发送一 个消息,用于处理响应手势动作后的任务。
不同的手势识别器,发送的消息方法也会有所不同。
使用注意事项:
1、实例化时,同时设置响应target对象,以及响应方法
2、响应方法中,可能会出现方法被多次响应的情况,可通过设置手势状态进行控制
3、同一个视图中既有单击,又有双击手势时,应该添加语句以进行识别" [singleTap requireGestureRecognizerToFail:doubleTap];"
// 图像视图
UIImageView *recognizerImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, 144, 60, 60)];
[recognizerImage setImage:[UIImage imageNamed:@"dingdang.png"]];
[recognizerImage setBackgroundColor:[UIColor clearColor]];
[recognizerImage setUserInteractionEnabled:YES];
[self.view addSubview:recognizerImage];
// 点击手势
// 单击的手势
UITapGestureRecognizer *tapRecognizerSingle = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRecognizerSingleAction:)];
tapRecognizerSingle.numberOfTapsRequired = 1; // 单击
// 图像视图添加单击手势
[recognizerImage addGestureRecognizer:tapRecognizerSingle];
// 双击的手势
UITapGestureRecognizer *tapRecognizerDouble = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRecognizerDoubleAction:)];
tapRecognizerDouble.numberOfTapsRequired = 2; // 双击
// 如果双击确定检测失败触发单击
[tapRecognizerSingle requireGestureRecognizerToFail:tapRecognizerDouble];
// 图像视图添加双击手势
[recognizerImage addGestureRecognizer:tapRecognizerDouble];
// 点击手势方法 单击
bool isChangeBgColor;
- (void)tapRecognizerSingleAction:(UITapGestureRecognizer *)recognizer
{
// 单击时改变背景颜色
if (1 == recognizer.numberOfTapsRequired)
{
UIImageView *view = (UIImageView *)recognizer.view;
if (!isChangeBgColor)
{
[view setBackgroundColor:[UIColor orangeColor]];
}
else
{
[view setBackgroundColor:[UIColor yellowColor]];
}
isChangeBgColor = !isChangeBgColor;
}
}
// 点击手势方法 双击
bool isChangeSize;
- (void)tapRecognizerDoubleAction:(UITapGestureRecognizer *)recognizer
{
// 放大或缩小视图
if (2 == recognizer.numberOfTapsRequired)
{
UIImageView *view = (UIImageView *)recognizer.view;
// 通过isChangeSize值区分放大还是缩小
if (!isChangeSize)
{
// 放大
CGRect currentBounds = view.bounds;
currentBounds.size.width *= 2;
currentBounds.size.height *= 2;
// 添加动画
[UIView animateWithDuration:0.3 animations:^{
view.bounds = currentBounds;
}];
}
else
{
// 缩小
CGRect currentBounds = view.bounds;
currentBounds.size.width /= 2;
currentBounds.size.height /= 2;
// 添加动画
[UIView animateWithDuration:0.3 animations:^{
view.bounds = currentBounds;
}];
}
isChangeSize = !isChangeSize;
}
}
// 长按手势
UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressRecognizerAction:)];
// 图像视图添加长按手势
[recognizerImage addGestureRecognizer:longPressRecognizer];
// 长按手势方法
- (void)longPressRecognizerAction:(UILongPressGestureRecognizer *)recognizer
{
// 改变选中视图背景色(原始背景色,原始大小)
UIImageView *view = (UIImageView *)recognizer.view;
[view setFrame:CGRectMake(0, 0, 60, 60)];
[view setCenter:CGPointMake(self.view.frame.size.width / 2, self.view.frame.size.height / 2)];
[view setBackgroundColor:[UIColor clearColor]];
}
// 旋转手势
UIRotationGestureRecognizer *rotateRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotateRecognizerAction:)];
// 图像视图添加旋转手势
[recognizerImage addGestureRecognizer:rotateRecognizer];
// 旋转手势方法
- (void)rotateRecognizerAction:(UIRotationGestureRecognizer *)recognizer
{
// 旋转视图
UIImageView *view = (UIImageView *)recognizer.view;
float rotationV = recognizer.rotation;
view.transform = CGAffineTransformMakeRotation(rotationV);
}
// 拖动手势
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panRecognizerAction:)];
// 图像视图添加拖动手势
[recognizerImage addGestureRecognizer:panRecognizer];
// 拖动手势方法
- (void)panRecognizerAction:(UIPanGestureRecognizer *)recognizer
{
// 拖动视图
UIImageView *view = (UIImageView *)recognizer.view;
CGPoint translation = [recognizer translationInView:view];
CGFloat centerX = view.center.x + translation.x;
CGFloat centerY = view.center.y + translation.y;
view.center = CGPointMake(centerX, centerY);
[recognizer setTranslation:CGPointZero inView:view];
}
// 滑动手势
// 滑动手势-向上滑
UISwipeGestureRecognizer *swipeRecognizerUP = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRecognizerAction:)];
swipeRecognizerUP.direction = UISwipeGestureRecognizerDirectionUp;
// 滑动手势-向下滑
UISwipeGestureRecognizer *swipeRecognizerDOWN = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRecognizerAction:)];
swipeRecognizerDOWN.direction = UISwipeGestureRecognizerDirectionDown;
// 滑动手势-向左滑
UISwipeGestureRecognizer *swipeRecognizerLEFT = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRecognizerAction:)];
swipeRecognizerLEFT.direction = UISwipeGestureRecognizerDirectionLeft;
// 滑动手势-向右滑
UISwipeGestureRecognizer *swipeRecognizerRIGHT = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRecognizerAction:)];
swipeRecognizerRIGHT.direction = UISwipeGestureRecognizerDirectionRight;
// 图像视图添加滑动手势
[recognizerImage addGestureRecognizer:swipeRecognizerUP];
[recognizerImage addGestureRecognizer:swipeRecognizerUP];
[recognizerImage addGestureRecognizer:swipeRecognizerUP];
[recognizerImage addGestureRecognizer:swipeRecognizerRIGHT];
// 滑动手势方法
- (void)swipeRecognizerAction:(UISwipeGestureRecognizer *)recognizer
{
UIImageView *view = (UIImageView *)recognizer.view;
NSLog(@"view(%@) %@", NSStringFromClass([view class]), view);
if (recognizer.direction == UISwipeGestureRecognizerDirectionUp)
{
NSLog(@"direction == UISwipeGestureRecognizerDirectionUp");
}
else if (recognizer.direction == UISwipeGestureRecognizerDirectionDown)
{
NSLog(@"direction == UISwipeGestureRecognizerDirectionDown");
}
else if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft)
{
NSLog(@"direction == UISwipeGestureRecognizerDirectionLeft");
}
else if (recognizer.direction == UISwipeGestureRecognizerDirectionRight)
{
NSLog(@"direction == UISwipeGestureRecognizerDirectionRight");
}
}
// 缩放手势
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchRecognizerAction:)];
// 图像视图添加缩放手势
[recognizerImage addGestureRecognizer:pinchRecognizer];
// 缩放手势方法
float lastScale = 0.0;
- (void)pinchRecognizerAction:(UIPinchGestureRecognizer *)recognizer
{
// 缩放视图
UIImageView *view = (UIImageView *)recognizer.view;
recognizer.scale = recognizer.scale - lastScale + 1.0;
view.transform = CGAffineTransformScale(view.transform, recognizer.scale, recognizer.scale);
lastScale = recognizer.scale;
}