ios各種手勢,很有意思

來源:互聯網
上載者:User

標籤:style   blog   http   color   使用   strong   

一、概述

iPhone中處理觸控螢幕的操作,在3.2之前是主要使用的是由UIResponder而來的如下4種方式:

 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event  - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event  - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event  - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

但是這種方式甄別不同的手勢操作實在是麻煩,需要你自己計算做不同的手勢分辨。後來。。。

蘋果就給出了一個比較簡便的方式,就是使用UIGestureRecognizer。

二、UIGestureRecognizer

UIGestureRecognizer基類是一個抽象類別,我們主要是使用它的子類(名字包含連結,可以點擊跳到ios Developer library,看官方文檔):

  • UITapGestureRecognizer

  • UIPinchGestureRecognizer

  • UIRotationGestureRecognizer

  • UISwipeGestureRecognizer

  • UIPanGestureRecognizer

  • UILongPressGestureRecognizer

從名字上我們就能知道, Tap(點擊)、Pinch(捏合)、Rotation(旋轉)、Swipe(滑動,快速移動,是用於監測滑動的方向的)、Pan (拖移,慢速移動,是用於監測位移的量的)以及 LongPress(長按)。

舉個例子,可以在viewDidLoad函數裡面添加:

 

[cpp] view plaincopy 
  1. -(void) viewDidLoad  
  2. {  
  3.  [super viewDidLoad];  
  4.  // Do any additional setup after loading the view from its nib.  
  5.  UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanFrom:)];  
  6.  [self.view addGestureRecognizer:panRecognizer];//關鍵語句,給self.view添加一個手勢監測;  
  7.  panRecognizer.maximumNumberOfTouches = 1;  
  8.  panRecognizer.delegate = self;  
  9.  [panRecognizer release];  
  10. }  

其它手勢方法類似。

 

其核心就是設定delegate和在需要手勢監測的view上使用addGestureRecognizer添加指定的手勢監測。

當然要記得在作為delegate的view的標頭檔加上<UIGestureRecognizerDelegate>。

不過有些手勢是關聯的,怎麼辦呢?例如 Tap 與 LongPress、Swipe與 Pan,或是 Tap 一次與Tap 兩次。

手勢識別是具有互斥的原則的,比如單擊和雙擊,如果它識別出一種手勢,其後的手勢將不被識別。所以對於關聯手勢,要做特殊處理以協助程式甄別,應該把當前手勢歸結到哪一類手勢裡面。

比如,單擊和雙擊並存時,如果不做處理,它就只能發送出單擊的訊息。為了能夠識別出雙擊手勢,就需要做一個特殊處理邏輯,即先判斷手勢是否是雙擊,在雙擊失效的情況下作為單擊手勢處理。使用

[A requireGestureRecognizerToFail:B]函數,它可以指定當A手勢發生時,即便A已經滿足條件了,也不會立刻觸發,會等到指定的手勢B確定失敗之後才觸發。

 

[cpp] view plaincopy 
  1. - (void)viewDidLoad   
  2. {  
  3.     // 單擊的 Recognizer  
  4.     UITapGestureRecognizer* singleRecognizer;  
  5.     singleRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:selfaction:@selector(SingleTap:)];  
  6.     //點擊的次數  
  7.     singleTapRecognizer.numberOfTapsRequired = 1; // 單擊  
  8.   
  9.     //給self.view添加一個手勢監測;  
  10.   
  11.   [self.view addGestureRecognizer:singleRecognizer];  
  12.   
  13.     
  14.     // 雙擊的 Recognizer  
  15.     UITapGestureRecognizer* double;  
  16.     doubleRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:selfaction:@selector(DoubleTap:)];  
  17.     doubleTapRecognizer.numberOfTapsRequired = 2; // 雙擊  
  18.     //關鍵語句,給self.view添加一個手勢監測;  
  19.     [self.view addGestureRecognizer:doubleRecognizer];  
  20.       
  21.     // 關鍵在這一行,雙擊手勢確定監測失敗才會觸發單擊手勢的相應操作  
  22.     [singleRecognizer requireGestureRecognizerToFail:doubleRecognizer];  
  23.     [singleRecognizer release];  
  24.     [doubleRecognizer release];  
  25. }  
  26.   
  27. -(void)SingleTap:(UITapGestureRecognizer*)recognizer  
  28. {  
  29. //處理單擊操作  
  30. }  
  31.   
  32. -(void)DoubleTap:(UITapGestureRecognizer*)recognizer  
  33. {  
  34. //處理雙擊操作  
  35. }  
三、iphone操作手勢的大概種類

1.點擊(Tap) 點擊作為最常用手勢,用於按下或選擇一個控制項或條目(類似於普通的滑鼠點擊)、

2.拖動(Drag) 拖動用於實現一些頁面的滾動,以及對控制項的移動功能。

3.滑動(Flick) 滑動用於實現頁面的快速滾動和翻頁的功能。

4.橫掃(Swipe) 橫掃手勢用於啟用清單項目的快捷操作菜單

5.雙擊(Double Tap) 雙擊放大並置中顯示圖片,或恢複原大小(如果當前已經放大)。同時,雙擊能夠啟用針對文字編輯菜單。

6.放大(Pinch open) 放大手勢可以實現以下功能:開啟訂閱源,開啟文章的詳情。在照片查看的時候,放大手勢也可實現放大圖片的功能。

7.縮小(Pinch close) 縮小手勢,可以實現與放大手勢相反且對應的功能的功能:關閉訂閱源退出到首頁,關閉文章退出至索引頁。在照片查看的時候,縮小手勢也可實現縮小圖片的功能。

8.長按(Touch &Hold) 在我的訂閱頁,長按訂閱源將自動進入編輯模式,同時選中手指當前按下的訂閱源。這時可直接拖動訂閱源移動位置。 針對文字長按,將出現放大鏡協助工具功能。鬆開後,則出現編輯菜單。 針對圖片長按,將出現編輯菜單。

9.搖晃(Shake) 搖晃手勢,將出現撤銷與重做菜單。主要是針對使用者文本輸入的。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.