1、UIGestureRecognizer 介绍
UIPanGestureRecognizer(拖动)
UIPinchGestureRecognizer(捏合)
UIRotationGestureRecognizer(旋转)
UITapGestureRecognizer(点按)
UILongPressGestureRecognizer(长按)
UISwipeGestureRecognizer(轻扫)
UIGestureRecognizer 的继承关系如下:
2、使用
//
// ViewController.m
// testOfGestureRecognizer
//
// Created by Bobby on 16/9/6.
// Copyright © 2016年 Bobby. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()<UIGestureRecognizerDelegate>
{
UIImageView * imageView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self createImageView];
[self createTapGesture];
[self createPinchGesture];
[self createRotationGesture];
[self cretePanGesture];
[self createLongPreessGesture];
[self createSwipGesture];
}
- (void)createImageView{
if (!imageView) {
imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"image"]];
[self.view addSubview:imageView];
imageView.frame = CGRectMake(100, 100, 150, 100);
imageView.center = self.view.center;
imageView.userInteractionEnabled = YES;
}
}
- (void)createTapGesture{
UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
// 点击次数 1表示单击 2 表示 双击(快速双击)
tapGesture.numberOfTapsRequired = 1;
// tapGesture.numberOfTapsRequired = 2;
//设置手指的个数
tapGesture.numberOfTouchesRequired = 2;
// 按住 option/alt 就 模拟器上就会出来两个小圆圈就相当于两个手指
[imageView addGestureRecognizer:tapGesture];
}
- (void)tapClick:(UITapGestureRecognizer *)tap {
self.view.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];
}
- (void)createPinchGesture{
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchClick:)];
//一个视图上可以加 多个手势的 不过我们一般加1个或者2两个
//加的太多了可能会产生影响的
pinch.delegate = self;
[imageView addGestureRecognizer:pinch];
}
//捏合就是 放大 / 缩小
- (void)pinchClick:(UIPinchGestureRecognizer *)pinch {
NSLog(@"pinch.scale:%f",pinch.scale);
//我们可以通过 transform 仿射变换 进行 放大 缩小
//可以pinch 的比例 scale
//相对于当前的_imageView.transform 进行缩放
imageView.transform = CGAffineTransformScale(imageView.transform, pinch.scale, pinch.scale);
//重置捏合手势的比例 每次都应该相对于当前的
pinch.scale = 1;//下次 相对比例应该从1 开始
}
- (void)createRotationGesture{
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotaion:)];
//如果要想两个手势同时生效那么需要设置代理
rotation.delegate = self;
[imageView addGestureRecognizer:rotation];
}
- (void)rotaion:(UIRotationGestureRecognizer *)rotation {
//transform 属性 可以设置旋转
//相对于 _imageView.transform 当前的弧度 进行 旋转
imageView.transform = CGAffineTransformRotate(imageView.transform, rotation.rotation);
//重置 旋转的弧度 相对于当前的 从0 开始
//重置0弧度
rotation.rotation = 0;
}
//拖动/移动手势
- (void)cretePanGesture{
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panClick:)];
//增加手势
[imageView addGestureRecognizer:pan];
}
- (void)panClick:(UIPanGestureRecognizer *)pan {
//获取 拖动手势的偏移量
//手势偏移量 相对的视图 要和 _imageView相对的坐标视图保持一致(相对的都是同一个视图)
CGPoint offSet = [pan translationInView:self.view];//相对于self.view的偏移量
//修改中心点位置
CGPoint point = imageView.center;
imageView.center = CGPointMake(point.x+offSet.x, point.y+offSet.y);
//把手势的偏移量 每次重置(0,0)CGPointZero
[pan setTranslation:CGPointZero inView:self.view];
}
- (void)createLongPreessGesture{
UILongPressGestureRecognizer *lPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
[imageView addGestureRecognizer:lPress];
}
/*
长按 手势 默认会触发两次 开始的时候一次 长按结束的时候再触发一次
*/
- (void)longPress:(UILongPressGestureRecognizer *)longPress {
//NSLog(@"长按手势");
//判断手势的状态
if (longPress.state == UIGestureRecognizerStateBegan) {//手势开始的时候
NSLog(@"长按手势开始");
}else if (longPress.state == UIGestureRecognizerStateEnded) {
//手势结束的状态
NSLog(@"长按手势结束");
}
}
- (void)createSwipGesture{
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
//滑动手势 需要设置 方向
//如果 需要四个方向有效 那么必须要加4手势在_imageView上
swipe.direction = UISwipeGestureRecognizerDirectionRight;
[imageView addGestureRecognizer:swipe];
}
- (void)swipe:(UISwipeGestureRecognizer *)swipe {
NSLog(@"滑动手势");
CGPoint center = imageView.center;
switch (swipe.direction) {
case UISwipeGestureRecognizerDirectionRight://向右
{
NSLog(@"向右滑动");
imageView.center = CGPointMake(center.x+100, center.y);
}
break;
case UISwipeGestureRecognizerDirectionLeft://向左
{
NSLog(@"向左滑动");
imageView.center = CGPointMake(center.x-100, center.y);
}
break;
case UISwipeGestureRecognizerDirectionUp://向上
{
NSLog(@"向上滑动");
imageView.center = CGPointMake(center.x, center.y-100);
}
break;
case UISwipeGestureRecognizerDirectionDown://向下
{
NSLog(@"向下滑动");
imageView.center = CGPointMake(center.x, center.y+100);
}
break;
default:
break;
}
}
//默认情况 增加的多个手势 同时只能有一个生效
/*
如果要想让两个没有相互影响的手势同时生效 那么必须要设置这两个手势的代理,两个手势委托代理来处理就可以了
代理需要遵守协议
比如 旋转和捏合没有相互影响可以设置同时生效 那么就需要设置旋转手势的代理和捏合手势的代理
同一个代理
*/
#pragma mark - UIGestureRecognizerDelegate协议
//是否允许 两个手势同时生效
//NO不允许
//YES允许
//两个手势 必须要设置代理
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
NSLog(@"first:%@",[gestureRecognizer class]);
NSLog(@"second:%@",[otherGestureRecognizer class]);
return YES;
}
@end