大家都知道,蘋果的設備,不管是mac機器還是iPhone或iad,都支持多點觸控,進而延伸了多種手勢識別的功能。這為用戶帶來了很大的便攜性和多樣靈活性,極大的方便了用戶的使用。足以見手勢識別(GestureRecognizers)在開發中也有舉足輕重的作用。
在iOS 4以前,手勢識別由開發人員負責。
主要使用的是由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
這往往需要進行復雜的數學運算才能甄別不用的手勢,蘋果意識到這種情況的復雜性和手勢對iPhone用戶界面的重要性之后,在iOS4中加入了UIGestureRecognizer類,使開發者更容易實現各種手勢的識別。
UIGestureRecognizer類是繼承自Nsobject的抽象類,它派生的6個主要子類分別詮釋了不同的手勢識別。
- UITapGestureRecognizer – “輕擊”手勢。可以配置為“單擊”和“連擊”的識別。
- UIPinchGestureRecognizer –“捏合”手勢。該手勢通常用於縮放視圖或改變可視組件的大小。
- UIPanGestureRecognizer – “平移”手勢。識別拖拽或移動動作。
- UISwipeGestureRecognizer – “輕掃”手勢。當用戶從屏幕上划過時識別為該手勢。可以指定該動作的方向(上、下、左、右)。
- UIRotationGestureRecognizer – “轉動”手勢。用戶兩指在屏幕上做相對環形運動。
- UILongPressGestureRecognizer – “長按”手勢。使用1指或多指觸摸屏幕並保持一定時間。
這些手勢識別器必需和視圖通過addGestureRecognizer:方法聯系在一起。識別器必需指定一個響應方法以便發生指定手勢時進行調用。removeGestureRecognizer:方法可以將識別器從視圖中移出,方法參數指定要移除的識別器.
識別器響應消息
iOS 4 的手勢識別器在偵測到某個手勢發生時,使用目標-動作模型去通知應用程序。當一個識別器被創建之后,只要有對應的手勢發生,就會調用創建時指定的方法。
連續和不連續手勢
手勢分為“連續手勢”和“不連續手勢”兩種。“不連續手勢”只會導致調用響應方法一次。“輕擊”(包括多擊)僅僅會觸發一次響應方法。而“輕掃”、“平移”、“旋轉”、“捏合”則是連續手勢,它們會連續不斷地調用響應方法直到手勢結束。
從手勢中獲取數據
每個手勢響應方法都會被傳入一個UIGestureRecognizer* sender對象,從中可以獲取到和手勢有關的信息。例如,對於“捏合”手勢,可以通過這個參數獲取縮放系數、捏合速度。對於“轉動”手勢,則可以獲得轉動的次數及速度。
識別輕擊手勢
輕擊手勢使用UITapGestureRecognizer類進行識別。在alloc和init時必需指定一個手勢觸發時調用的方法引用(即selector選擇器)。使用numberOfTapsRequired屬性可以定義輕擊必需被連續操作的次數。如以下代碼所示,當識別器偵測到連續輕擊2次時,將觸發tapDetected:方法。
-(void) tapGestureRecognizeTest
{
UITapGestureRecognizer *doubleTap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDeteted:)];
doubleTap.numberOfTapsRequired=2;
[self.parentUiView addGestureRecognizer:doubleTap];
[doubleTap release];
}
-(IBAction)tapDeteted:(UIGestureRecognizer *)sender
{
NSLog(@"雙擊手勢");
}
識別捏合手勢
捏合手勢使用UIPinchGestureRecognizer類識別。例如:
-(void) pinchGestureRecongnizeTest
{
UIPinchGestureRecognizer *pinshGesture=[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinshDeteted:)];
[self.parentUiView addGestureRecognizer:pinshGesture];
[pinshGesture release];
}
-(IBAction)pinshDeteted:(UIGestureRecognizer *)sender
{
NSLog(@"此觸摸手勢為%@",@"捏合手勢");
}
識別轉動手勢
轉動手勢使用UIRotationGestureRecognizer類。
UIRotationGestureRecognizer *rotationRecognizer =
[[UIRotationGestureRecognizer alloc]
initWithTarget:self
action:@selector(rotationDetected:)];
[self.view addGestureRecognizer:rotationRecognizer];
[rotationRecognizer release];
識別平移手勢
平移手勢使用UIPanGestureRecognizer 類。平移手勢是最基本的連續手勢。例如手指在屏幕上隨意亂划可以被識別為平移或拖拽操作:
UIRotationGestureRecognizer *panRecognizer =
[[UIPanGestureRecognizer alloc]
initWithTarget:self
action:@selector(panDetected:)];
[self.view addGestureRecognizer:panRecognizer];
[panRecognizer release];
如果輕掃和平移都添加在同一個view中,那么很可能大部分輕掃手勢都會被識別為平移。而且如果兩種手勢被添加到同一個view時,會產生一個警告。
識別輕掃手勢
輕掃手勢使用 UISwipeGestureRecognizer 類。所有的輕掃都將被識別,或者你可以指定只偵測方向為以下常量的輕掃:
- UISwipeGestureRecognizerDirectionRight
- UISwipeGestureRecognizerDirectionLeft
- UISwipeGestureRecognizerDirectionUp
- USwipeIGestureRecognizerDirectionDown
如果不指定direction屬性,默認只偵測方向為右的輕掃。以下代碼設置只偵測向上輕掃:
UISwipeGestureRecognizer *swipeRecognizer =
[[UISwipeGestureRecognizer alloc]
initWithTarget:self
action:@selector(swipeDetected:)];
swipeRecognizer.direction = UISwipeGestureRecognizerDirectionUp; [self.view addGestureRecognizer:swipeRecognizer];
[swipeRecognizer release];
識別長按手勢
長按手勢使用 UILongPressGestureRecognizer 類。這個手勢需要指定長按的時間,觸摸的次數,點擊的次數以及在觸摸過程中是否允許移動。這些選項分別由minimumPressDuration, numberOfTouchesRequired, numberOfTapsRequired 和allowableMovement 屬性指定。以下代碼使識別器只偵測單指長按3秒以上的手勢。allowableMovement未指定,默認是允許10個像素的移動:
UILongPressGestureRecognizer *longPressRecognizer =
[[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:@selector(longPressDetected:)]; longPressRecognizer.minimumPressDuration = 3;
longPressRecognizer.numberOfTouchesRequired = 1;
[self.view addGestureRecognizer:longPressRecognizer];
[longPressRecognizer release];
注:ios模擬器使用捏合手勢時,按住option鍵。然后結合鼠標模擬。 觸摸手勢說明: (1)右鍵按住+移動鼠標向左=放大; (2)右鍵按住+移動鼠標向右=縮小; (3)右鍵按住+移動鼠標向上=前傾; (4)右鍵按住+移動鼠標向下=后傾