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