ios 手勢 常用程式碼片段程式碼片段

來源:互聯網
上載者:User

ios 手勢 程式碼片段 轉http://www.oschina.net/code/snippet_54100_8106

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));

}
[代碼] 向裡或向外捏的手勢
view sourceprint?

UIPinchGestureRecognizer *twoFingerPinch =

  [[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingerPinch:)] autorelease];

[[self view] addGestureRecognizer:twoFingerPinch];

- (void)twoFingerPinch:(UIPinchGestureRecognizer *)recognizer

{

  NSLog(@"Pinch scale: %f", recognizer.scale);
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.