/** 1. 使用手势识别的四个步骤 1> 实例化手势识别 - (id)initWithTarget:(id)target action:(SEL)action; 2> 设置手势识别属性 3> 将手势识别附加到指定的视图 addGestureRecognizer 4> 编写监听方法 用代码创建一个UIImageView添加到视图,实验6种手势识别。 3. 手势说明 0> UIGestureRecognizer 所有手势识别的父类,不允许直接使用,可以用来自定义手势(建议大家不要挑战) 最常用的属性: view: 发生手势的视图 state: 手势当前的状态,主要用于连续手势,对于离散手势一般不使用 1> UITapGestureRecognizer 点按手势(离散手势,其他手势都是连续手势) 属性: numberOfTapsRequired 点击次数,单击双击 numberOfTouchesRequired 手指根数 2> UILongPressGestureRecognizer 长按手势 属性不建议修改 3> UIPanGestureRecognizer 拖动手势 属性:不常用 方法: // 在视图中拖动的距离 - (CGPoint)translationInView:(UIView *)view; // 在视图中拖动的速度,通常可用于模拟惯性,需要一些物理方面的计算 - (CGPoint)velocityInView:(UIView *)view; 4> UIPinchGestureRecognizer 捏合手势 属性: scale 比例 velocity 捏合速度,不常用 5> UIRotationGestureRecognizer 旋转手势 属性 rotation 旋转角度 velocity 旋转速度,不常用 6> UISwipeGestureRecognizer 轻扫手势,通常添加到根视图上 属性 numberOfTouchesRequired 参与轻扫手势的手指根数 direction 轻扫的方向 提示: 1) 如果要检测几个方向的轻扫,需要分别实例化几个轻扫手势 2) 轻扫手势虽然是连续手势,但是不需要去处理UIGestureRecognizerStateChanged状态 因为是在手指离开屏幕后,该手势才被识别的 其他技巧 在if语句中,将常量放置在判断条件的左侧,可以避免因为只写了 = 出现警告,置之不理的情况! */- (void)viewDidLoad{ [super viewDidLoad]; // 0. 实例化UIImageView UIImage *image = [UIImage imageNamed:@"001"]; UIImageView *imageView = [[UIImageView alloc]initWithImage:image]; [imageView setFrame:CGRectMake(10, 100, 300, 196)]; [self.view addSubview:imageView]; [imageView setUserInteractionEnabled:YES]; // 1. 添加点按手势 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)]; // 定义双击 tap.numberOfTapsRequired = 2; [imageView addGestureRecognizer:tap]; // 2. 添加长按手势 UILongPressGestureRecognizer *longTap = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longTap:)]; [imageView addGestureRecognizer:longTap]; // 3. 拖动手势 UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)]; [imageView addGestureRecognizer:pan]; // 4. 捏合手势 UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)]; [imageView addGestureRecognizer:pinch]; // 5. 旋转手势 UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)]; [imageView addGestureRecognizer:rotation]; // 6. 轻扫手势 // 如果要做清扫手势,需要分别实例化4个方向的手势 UISwipeGestureRecognizer *swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)]; swipeUp.direction = UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight; [self.view addGestureRecognizer:swipeUp]; // UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];// swipeDown.direction = UISwipeGestureRecognizerDirectionDown;// [self.view addGestureRecognizer:swipeDown];// // UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];// swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;// [self.view addGestureRecognizer:swipeLeft];//// UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];// swipeRight.direction = UISwipeGestureRecognizerDirectionRight;// [self.view addGestureRecognizer:swipeRight];}#pragma mark - 点按手势- (void)tap:(UITapGestureRecognizer *)recognizer{ // 双击放大图片 UIImageView *imageView = (UIImageView *)recognizer.view; [UIView animateWithDuration:0.8f animations:^{ [imageView setTransform:CGAffineTransformScale(imageView.transform, 2.0, 2.0)]; } completion:^(BOOL finished) { [UIView animateWithDuration:0.5f animations:^{ [imageView setTransform:CGAffineTransformScale(imageView.transform, 0.5, 0.5)]; }]; }];}#pragma mark - 长按手势- (void)longTap:(UILongPressGestureRecognizer *)recognizer{ NSLog(@"长按"); // 长按旋转图片 // 开始长按 // 在形变时,iOS采取就近原则旋转,如果不能按照希望的方向旋转,可以增加一些修正值,例如0.01 // 直接通过形变属性,要实现一次性转一圈,比较困难,可以分两次进行 if (UIGestureRecognizerStateBegan == recognizer.state) { [UIView animateWithDuration:0.8f animations:^{ [recognizer.view setTransform:CGAffineTransformRotate(recognizer.view.transform, M_PI - 0.01)]; }]; } else if (UIGestureRecognizerStateEnded == recognizer.state) { [UIView animateWithDuration:0.8f animations:^{ [recognizer.view setTransform:CGAffineTransformRotate(recognizer.view.transform, M_PI - 0.01)]; } completion:^(BOOL finished) { // 清除所有的形变属性 [recognizer.view setTransform:CGAffineTransformIdentity]; }]; }}#pragma mark - 拖动- (void)pan:(UIPanGestureRecognizer *)recognizer{ CGPoint translation = [recognizer translationInView:self.view]; // 让图像根据手指移动 if (UIGestureRecognizerStateChanged == recognizer.state) { // 形变参数,有Make的时候,是相对初始位置计算的 // 没有Make是递增修改形变的 [recognizer.view setTransform:CGAffineTransformTranslate(recognizer.view.transform, translation.x, translation.y)]; // 拖动手指中的平移距离是相对于初始位置,如果使用CGAffineTransformTranslate累加形变方法 // 需要在每次位移之后,重置recognizer的位移量,就是将位移量清零 [recognizer setTranslation:CGPointZero inView:recognizer.view]; }}#pragma mark - 捏合- (void)pinch:(UIPinchGestureRecognizer *)recognizer{ // 调整图像的显示比例 if (UIGestureRecognizerStateChanged == recognizer.state) { // 等比例缩放 [recognizer.view setTransform:CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale)]; // 在设置累加形变参数时,需要充值手势的缩放比例 recognizer.scale = 1.0f; }}#pragma mark - 旋转- (void)rotate:(UIRotationGestureRecognizer *)recognizer{ if (UIGestureRecognizerStateChanged == recognizer.state) { recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation); // 重置旋转量 recognizer.rotation = 0.0f; }}#pragma mark - 轻扫- (void)swipe:(UISwipeGestureRecognizer *)recognizer{ //NSLog(@"%d", recognizer.direction); if (UISwipeGestureRecognizerDirectionDown == recognizer.direction) { NSLog(@"向下"); } else if (UISwipeGestureRecognizerDirectionUp == recognizer.direction) { NSLog(@"向上"); } else if (UISwipeGestureRecognizerDirectionLeft == recognizer.direction) { NSLog(@"向左"); } else if (UISwipeGestureRecognizerDirectionRight == recognizer.direction) { NSLog(@"向右"); }}