IOS中手勢操做通常是 UIGestureRecognizer 類的幾個手勢子類去實現,通常咱們用到的手勢就這麼5種:ui
一、點擊 UITapGestureRecognizeratom
二、平移 UIPanGestureRecognizerspa
三、縮放 UIPinchGestureRecognizer3d
四、旋轉 UIRotationGestureRecognizer日誌
五、輕掃 UISwipeGestureRecognizercode
咱們上面這個實例中就用到了上面這5種手勢,不過其中 點擊與輕掃沒有體現出來,只是輸出了下日誌而已,一會看代碼orm
下面咱們來分別介紹下這幾種手勢對象
UITapGestureRecognizer* tapGes = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGes:)]; // 點擊次數,默認爲1,1爲單擊,2爲雙擊 tapGes.numberOfTapsRequired = 2;
這個點擊手勢類有一個屬性 numberOfTapsRequired 用於設置點擊數,就是點擊幾回才觸發這個事件blog
// 平移手勢 - (void)initPanGes{ UIPanGestureRecognizer* panGes = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGes:)]; [self.imgView addGestureRecognizer:panGes]; } - (void)panGes:(UIPanGestureRecognizer*)ges{ // 獲取平移的座標點 CGPoint transPoint = [ges translationInView:self.imgView]; }
平移手勢自己沒太多可設置的屬性,在平移事件觸發手,能夠用 translationInView 方法獲取當前平移座標點事件
// 縮放手勢 - (void)initPinGes{ UIPinchGestureRecognizer* pinGes = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinGes:)]; [self.imgView addGestureRecognizer:pinGes]; } - (void)pinGes:(UIPinchGestureRecognizer*)ges{ // 縮放 self.imgView.transform = CGAffineTransformScale(self.imgView.transform, ges.scale, ges.scale); }
縮放手勢在事件裏面能夠獲取 scale 屬性,表示當前縮放值
// 旋轉手勢 - (void)initRotation{ UIRotationGestureRecognizer* rotationGes = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationGes:)]; [self.imgView addGestureRecognizer:rotationGes]; } - (void)rotationGes:(UIRotationGestureRecognizer*)ges{ // 旋轉圖片 self.imgView.transform = CGAffineTransformRotate(self.imgView.transform, ges.rotation); }
旋轉手勢在事件裏面能夠經過獲取 rotation 屬性獲取當前旋轉的角度
// 輕掃手勢 - (void)initSwipeGes{ // 建立 從右向左 輕掃的手勢 UISwipeGestureRecognizer* swipeLeftGes = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeGes:)]; // 方向,默認是從左往右 // 最多隻能開啓一個手勢,若是要開啓多個就得建立多個手勢 // 監遵從右向左的方向 swipeLeftGes.direction = UISwipeGestureRecognizerDirectionLeft; [self.imgView addGestureRecognizer:swipeLeftGes]; } - (void)swipeGes:(UISwipeGestureRecognizer*)ges{ // ges.direction方向值 NSLog(@"%s diection:%lu",__func__,(unsigned long)ges.direction); }
輕掃手勢對象須要設置 direction 屬性,默認是隻監遵從左向右,這是一個枚舉值 UISwipeGestureRecognizerDirection
UISwipeGestureRecognizerDirectionRight 從左向右(默認值)
UISwipeGestureRecognizerDirectionLeft 從右向左
UISwipeGestureRecognizerDirectionUp 從下向上
UISwipeGestureRecognizerDirectionDown 從上向下
下面看一下咱們上面那個效果圖實現代碼吧
// // ViewController.m // 各類手勢操做 // // Created by xgao on 16/3/24. // Copyright © 2016年 xgao. All rights reserved. // #import "ViewController.h" @interface ViewController ()<UIGestureRecognizerDelegate> @property (weak, nonatomic) IBOutlet UIImageView *imgView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self initTapGes]; [self initPanGes]; [self initPinGes]; [self initRotation]; [self initSwipeGes]; } // 點擊手勢 - (void)initTapGes{ UITapGestureRecognizer* tapGes = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGes:)]; // 點擊次數,默認爲1,1爲單擊,2爲雙擊 tapGes.numberOfTapsRequired = 2; tapGes.delegate = self; [self.imgView addGestureRecognizer:tapGes]; } - (void)tapGes:(UITapGestureRecognizer*)ges{ NSLog(@"%s",__func__); } // 平移手勢 - (void)initPanGes{ UIPanGestureRecognizer* panGes = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGes:)]; panGes.delegate = self; [self.imgView addGestureRecognizer:panGes]; } - (void)panGes:(UIPanGestureRecognizer*)ges{ // 獲取平移的座標點 CGPoint transPoint = [ges translationInView:self.imgView]; // 在以前的基礎上移動圖片 self.imgView.transform = CGAffineTransformTranslate(self.imgView.transform, transPoint.x, transPoint.y); // 復原,必需復原 // 每次都清空一下消除座標疊加 [ges setTranslation:CGPointZero inView:self.imgView]; NSLog(@"%s",__func__); } // 縮放手勢 - (void)initPinGes{ UIPinchGestureRecognizer* pinGes = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinGes:)]; pinGes.delegate = self; [self.imgView addGestureRecognizer:pinGes]; } - (void)pinGes:(UIPinchGestureRecognizer*)ges{ // 縮放 self.imgView.transform = CGAffineTransformScale(self.imgView.transform, ges.scale, ges.scale); // 復原 // 每次都清空一下消除疊加 ges.scale = 1; } // 旋轉手勢 - (void)initRotation{ UIRotationGestureRecognizer* rotationGes = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationGes:)]; rotationGes.delegate = self; [self.imgView addGestureRecognizer:rotationGes]; } - (void)rotationGes:(UIRotationGestureRecognizer*)ges{ // 旋轉圖片 self.imgView.transform = CGAffineTransformRotate(self.imgView.transform, ges.rotation); // 復原 // 每次都清空一下消除疊加 ges.rotation = 0; NSLog(@"%s",__func__); } // 輕掃手勢 - (void)initSwipeGes{ // 建立 從右向左 輕掃的手勢 UISwipeGestureRecognizer* swipeLeftGes = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeGes:)]; // 方向,默認是從左往右 // 最多隻能開啓一個手勢,若是要開啓多個就得建立多個手勢 // 監遵從右向左的方向 swipeLeftGes.direction = UISwipeGestureRecognizerDirectionLeft; swipeLeftGes.delegate = self; [self.imgView addGestureRecognizer:swipeLeftGes]; // 建立 從下向上 輕掃的手勢 UISwipeGestureRecognizer* swipeUpGes = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeGes:)]; // 監遵從下向上的方向 swipeUpGes.direction = UISwipeGestureRecognizerDirectionUp; swipeUpGes.delegate = self; [self.imgView addGestureRecognizer:swipeUpGes]; } - (void)swipeGes:(UISwipeGestureRecognizer*)ges{ // ges.direction方向值 NSLog(@"%s diection:%lu",__func__,(unsigned long)ges.direction); } #pragma mark - UIGestureRecognizerDelegate // 判斷是否能觸發手勢 - (BOOL)gestureRecognizerShouldBegin:(UITapGestureRecognizer *)gestureRecognizer{ return YES; } // 是否容許多手勢操做,不是多觸摸點 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{ return YES; } @end
這裏須要注意的有兩點:
一、對於 平移、縮放、旋轉 這3個手勢,咱們若是要用它的值去處理的話,要記得復原!復原!復原!這點很重要!重要的事說3遍~~
平移手勢裏面咱們須要設置 setTranslation:CGPointZero 來複原它的座標值,否則下一次事件觸發這個座標值會疊加
縮放手勢裏面設置 ges.scale = 1 來複原它的縮放值
旋轉手勢裏面設置 ges.rotation = 0 來複原它的角度值
二、假如咱們須要多手勢一塊兒用的時候就須要設置下delegate 裏面的一個返回參數的方法了
// 是否容許多手勢操做,不是多觸摸點 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{ return YES; }
此次分享就到這裏~~有什麼不清楚的,就留言吧 ^_^