1 /** 2 UIKit中包含了UIGestureRecognizer類,用於檢測發生在設備中手勢。 3 UIGestureRecognizer是一個抽象類,定義了全部手勢的基本行爲,他有下面一些子類用於處理具體的手勢: 4 1.拍擊UITapGestureRecognizer(任意次數的拍擊) 5 2.向裏或向外捏 UIPinchGestureRecognizer(用於縮放) 6 3.搖動或者拖拽 UIPanGestureRecognizer 7 4.滑動 UISwipeGestureRecoginzer(以任意方向) 8 5.旋轉 UIRotationGestureRecoginzer (手指朝相反方向移動) 9 6.長按 UILongPressGestureRecoginzer 10 11 對於不一樣類型的手勢識別器,具備不一樣的配置屬性。好比UITapGestureRecognizer,能夠配置拍擊次數。界面接收到手勢以後,能夠發送一 個消息,用於處理響應手勢動做後的任務。固然,不一樣的手勢識別器,發送的消息方法也會有所不一樣。下面列舉幾個具體示例代碼: 12 */ 13 14 15 #pragma mark 一個手指,拍擊兩次手勢 16 -(void)test1{ 17 UITapGestureRecognizer *oneFingerTwoTaps = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(oneFingerTwoTaps)]; 18 // Set required taps and number of touches 19 //敲擊次數 隨意設,不過通常狀況都是很少於2此 20 [oneFingerTwoTaps setNumberOfTapsRequired:2]; 21 //敲擊手指數 隨意設,不過要考慮用戶使用 22 [oneFingerTwoTaps setNumberOfTouchesRequired:1]; 23 24 // Add the gesture to the view 25 //添加到View 26 [self.view addGestureRecognizer:oneFingerTwoTaps]; 27 } 28 //消息方法 29 -(void)oneFingerTwoTaps 30 { 31 NSLog(@"敲擊了。。。"); 32 } 33 34 #pragma mark 一個手指滑動手勢 35 -(void)test2 36 { 37 UISwipeGestureRecognizer *oneFingerSwipeUp = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(oneFingerSwipeUp:)]; 38 39 40 //滑動方向<#(UISwipeGestureRecognizerDirection)#>枚舉類型 41 /* 42 UISwipeGestureRecognizerDirectionRight = 1 << 0, 43 UISwipeGestureRecognizerDirectionLeft = 1 << 1, 44 UISwipeGestureRecognizerDirectionUp = 1 << 2, 45 UISwipeGestureRecognizerDirectionDown = 1 << 3 46 */ 47 [oneFingerSwipeUp setDirection:UISwipeGestureRecognizerDirectionUp ]; 48 [self.view addGestureRecognizer:oneFingerSwipeUp]; 49 } 50 //消息方法 51 -(void)oneFingerSwipeUp:(UISwipeGestureRecognizer *)recognizer 52 { 53 CGPoint point = [recognizer locationInView:self.view]; 54 NSLog(@"上滑 - start location: %f,%f", point.x, point.y); 55 } 56 57 #pragma mark 旋轉手勢 58 -(void)test3 59 { 60 UIRotationGestureRecognizer *twoFingersRoatate = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(twoFingersRotate:)]; 61 [self.view addGestureRecognizer:twoFingersRoatate]; 62 63 } 64 -(void)twoFingersRotate:(UIRotationGestureRecognizer *)recognizer 65 { 66 NSLog(@"Rotation in degrees since last change: %f", [recognizer rotation] * (180 / M_PI)); 67 }