IOS 手勢、手勢響應器UIGestureRecognizer

手勢,手勢響應GestureRecognizer

關於手勢響應IOS中封裝了一個類,能響應一般手勢UIGestureRecognizer 


下面說一下這個UIGestureRecognizer 類
這個類有幾個子類,這幾個子類分別表示幾種不同的基本手勢
1、UITapGestureRecognizer  //點擊手勢識別器,可以是點擊一次,或多次都能識別
2、UIPinchGestureRecognizer  //捏合手勢識別器,用於視圖的放大縮小
3、UIRotationGestureRecognizer //旋轉手勢識別器
4、UISwipeGestureRecognizer   //滑動手勢識別器,向上、下、左、右滑動
5、UIPanGestureRecognizer      //拖動手勢識別器
6、UILongPressGestureRecognizer   //長按手勢識別器,常見的有長按跳出一個界面用以編輯


首先說一下手勢用法的基本流程
1、創建手勢識別器,這樣才能獲取手勢識別器能處理的手勢
2、配置能識別的具體手勢,如是能識別向左滑還是相右滑
3、把手勢識別器添加到視圖中


一、首先是點擊手勢
其實點擊分爲兩種情況一種是單擊,另一種是多擊(一般只用到雙擊)
下面就創建一個能響應單擊手勢的識別器


//創建點擊手勢識別器
    UITapGestureRecognizer *pSingleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleGesture:)];
   //設置這個點擊手勢識別器所能響應的點擊次數,單擊所以設成1
    pSingleTap.numberOfTapsRequired = 1;
    //添加到視圖中
    [self.view addGestureRecognizer:pSingleTap];
    其中singleGesture:這個就是當手勢識別器識別了手勢後所調用的方法,
    這個方法是自己寫的,用於處理響應後的操作
    
    這樣就三話就創建了一個可以識別單擊的識別器。很方便
    
    下面再創建一個雙擊手勢識別器,跟單擊基本上一樣,只是多了一行代碼
       //創建雙擊
    UITapGestureRecognizer *pDoubuleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doubleGesture:)];
    pDoubuleTap.numberOfTapsRequired = 2;
    [self.view addGestureRecognizer:pDoubuleTap];
    
    [pSingleTap requireGestureRecognizerToFail:pDoubuleTap];
    
   在這裏爲什麼要加上最後一行代碼呢?
   如果不加最後一行代碼的話,當你雙擊的時候雖然雙擊手勢識別器識別了這個手勢,
   並且調用方法doubleGesture:響應了雙擊手勢,但是問題是,在響應這個手勢的時候
   單擊手勢也再響應,並響應了兩次。
   正式爲了解決這個問題所以纔有了最後一行代碼,下面說一下這行代碼到底什麼意思
   首先看這個方法的英文就可以大概得出:手勢識別器pSingleTap  是在pDoubuleTap識別失敗的時候才識別的。
   也就是說當有一個點擊手勢時首先查看這個手勢是否是pDoubuleTap能識別處理的手勢,若pDoubuleTap能
   識別這個手勢,那麼就不再請求pSingleTap 來識別,相反,若pDoubuleTap不能識別,則再請求看pSingleTap
   能不能識別,這樣就分開了對這個手勢的雙重響應
   
   那要是再加一個三擊的手勢識別器呢,只需把最後一句改一下就行
       [pDoubuleTap requireGestureRecognizerToFail:pThreeTap];
       
       相信應該很好理解了,先看pThreeTap能不能識別,不能識別再看pDoubuleTap能不能識別,最後
       pDoubuleTap不能識別的話再請求pSingleTap手勢識別器來識別
       
       點擊就說這麼多吧
       


二、滑動手勢UISwipeGestureRecognizer   //滑動手勢識別器,向上、下、左、右滑動


下面創建一個滑動手勢識別器


 //清掃
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeGesture:)];
    //設置能識別滑動手勢的方向,
    swipe.direction = UISwipeGestureRecognizerDirectionLeft;
    swipe.direction = UISwipeGestureRecognizerDirectionRight;
    swipe.direction = UISwipeGestureRecognizerDirectionUp;
    swipe.direction = UISwipeGestureRecognizerDirectionDown;
    //注意一個手勢識別器只能識別一個方向上的滑動
    [self.view addGestureRecognizer:swipe];
    
三、長按手勢UILongPressGestureRecognizer
創建長按手勢識別器
////////////長按
    UILongPressGestureRecognizer *pLongPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
    //設置長按多少秒後才識別這個手勢
    pLongPress.minimumPressDuration = 2;
    [self.view addGestureRecognizer:pLongPress];
    
四、移動手勢UIPanGestureRecognizer
創建
 UIPanGestureRecognizer *pPan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGesture:)];
    
    [self.view addGestureRecognizer:pPan];
可以根據平移時的位置的移動來拖動視圖
獲取位置    CGPoint point = [pPan locationInView:self.view];
用這個方法可以獲取到當前移動到得位置


五、捏合手勢UIPinchGestureRecognizer
捏合有兩個屬性
@property (nonatomic,readonly) CGFloat velocity;  //捏合的速度


@property (nonatomic)          CGFloat scale;   //比例(經常用到放縮比例)
這個屬性默認值是1,通過獲取放縮比例屬性


創建
   //添加捏合手勢
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(doPinch:)];
    pinch.delegate = self;  //設置委託,這個委託屬性是屬於UIGestureRecognizer 的
    [self.view addGestureRecognizer:pinch];



六、旋轉手勢UIRotationGestureRecognizer
旋轉也由兩個屬性
@property (nonatomic)          CGFloat rotation;  //旋轉角度


@property (nonatomic,readonly) CGFloat velocity;  //旋轉速度
這個主要通過獲取旋轉角度來完成一些列操作


//添加旋轉手勢識別器
    UIRotationGestureRecognizer *rotat = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(doRotate:)];
    rotat.delegate =self;
    [self.view addGestureRecognizer:rotat];
    
    在後面我回附上一個捏合和旋轉手勢完成的一個demo,就是放縮和旋轉一個圖片
    

發佈了0 篇原創文章 · 獲贊 1 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章