你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> 如何使用iOS手勢UIGestureRecognizer

如何使用iOS手勢UIGestureRecognizer

編輯:IOS開發綜合
UIKit中包含了UIGestureRecognizer類,用於檢測發生在設備中的手勢。UIGestureRecognizer是一個抽象類,定義了所有手勢的基本行為,它有下面一些子類用於處理具體的手勢:        1、拍擊UITapGestureRecognizer (任意次數的拍擊)       2、向裡或向外捏UIPinchGestureRecognizer (用於縮放)       3、搖動或者拖拽UIPanGestureRecognizer       4、滑動UISwipeGestureRecognizer (以任意方向)       5、旋轉UIRotationGestureRecognizer (手指朝相反方向移動)       6、長按UILongPressGestureRecognizer    對於不同類型的手勢識別器,具有不同的配置屬性。比如UITapGestureRecognizer,可以配置拍擊次數。界面接收到手勢之後,可以發送一 個消息,用於處理響應手勢動作後的任務。當然,不同的手勢識別器,發送的消息方法也會有所不同。下面列舉幾個具體示例代碼:       一個手指,拍擊兩次手勢   // 創建一個手勢識別器 UITapGestureRecognizer *oneFingerTwoTaps =   [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerTwoTaps)] autorelease];   // Set required taps and number of touches [oneFingerTwoTaps setNumberOfTapsRequired:2]; [oneFingerTwoTaps setNumberOfTouchesRequired:1];   // Add the gesture to the view [[self view] addGestureRecognizer:oneFingerTwoTaps];   消息方法oneFingerTwoTaps - (void)oneFingerTwoTaps {   NSLog(@"Action: One finger, two taps"); }       兩個手指,拍擊兩次手勢   UITapGestureRecognizer *twoFingersTwoTaps =   [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingersTwoTaps)] autorelease]; [twoFingersTwoTaps setNumberOfTapsRequired:2]; [twoFingersTwoTaps setNumberOfTouchesRequired:2]; [[self view] addGestureRecognizer:twoFingersTwoTaps];   消息方法twoFingersTwoTaps - (void)twoFingersTwoTaps {   NSLog(@"Action: Two fingers, two taps"); }        一個手指向上、向下滑動手勢   // 向上滑動 UISwipeGestureRecognizer *oneFingerSwipeUp =   [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerSwipeUp:)] autorelease]; [oneFingerSwipeUp setDirection:UISwipeGestureRecognizerDirectionUp]; [[self view] addGestureRecognizer:oneFingerSwipeUp];   - (void)oneFingerSwipeUp:(UISwipeGestureRecognizer *)recognizer {   CGPoint point = [recognizer locationInView:[self view]];   NSLog(@"Swipe up - start location: %f,%f", point.x, point.y); }   // 向下滑動 UISwipeGestureRecognizer *oneFingerSwipeDown =   [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerSwipeDown:)] autorelease]; [oneFingerSwipeDown setDirection:UISwipeGestureRecognizerDirectionDown]; [[self view] addGestureRecognizer:oneFingerSwipeDown];   - (void)oneFingerSwipeDown:(UISwipeGestureRecognizer *)recognizer {   CGPoint point = [recognizer locationInView:[self view]];   NSLog(@"Swipe down - start location: %f,%f", point.x, point.y); }       旋轉手勢   UIRotationGestureRecognizer *twoFingersRotate =   [[[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingersRotate:)] autorelease]; [[self view] addGestureRecognizer:twoFingersRotate];   - (void)twoFingersRotate:(UIRotationGestureRecognizer *)recognizer {   // Convert the radian value to show the degree of rotation   NSLog(@"Rotation in degrees since last change: %f", [recognizer rotation] * (180 / M_PI)); }       向裡或向外捏的手勢   UIPinchGestureRecognizer *twoFingerPinch =   [[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingerPinch:)] autorelease]; [[self view] addGestureRecognizer:twoFingerPinch];   - (void)twoFingerPinch:(UIPinchGestureRecognizer *)recognizer {   NSLog(@"Pinch scale: %f", recognizer.scale); }                       UITapGestureRecognizer *twoFingersTwoTaps =   [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingersTwoTaps)] autorelease]; [twoFingersTwoTaps setNumberOfTapsRequired:2]; [twoFingersTwoTaps setNumberOfTouchesRequired:2]; [[self view] addGestureRecognizer:twoFingersTwoTaps];   消息方法twoFingersTwoTaps - (void)twoFingersTwoTaps {   NSLog(@"Action: Two fingers, two taps"); } 
  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved