你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> IOS手勢

IOS手勢

編輯:IOS開發綜合

UIKit中包含了UIGestureRecognizer類,用於檢測發生在設備中的手勢。UIGestureRecognizer是一個抽象類,定義了所有手勢的基本行為,它有下面一些子類用於處理具體的手勢:

    1、拍擊UITapGestureRecognizer (任意次數的拍擊) 
    2、向裡或向外捏UIPinchGestureRecognizer (用於縮放) 
    3、搖動或者拖拽UIPanGestureRecognizer 
    4、擦碰UISwipeGestureRecognizer (以任意方向) 
    5、旋轉UIRotationGestureRecognizer (手指朝相反方向移動) 
    6、長按UILongPressGestureRecognizer

對於不同類型的手勢識別器,具有不同的配置屬性。比如UITapGestureRecognizer,可以配置拍擊次數。界面接收到手勢之後,可以發送一個消息,用於處理響應手勢動作後的任務。當然,不同的手勢識別器,發送的消息方法也會有所不同。
[代碼] 一個手指,拍擊兩次手勢
01
// 創建一個手勢識別器
02
UITapGestureRecognizer *oneFingerTwoTaps =
03
  [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerTwoTaps)] autorelease];
04
 
05
// Set required taps and number of touches
06
[oneFingerTwoTaps setNumberOfTapsRequired:2];
07
[oneFingerTwoTaps setNumberOfTouchesRequired:1];
08
 
09
// Add the gesture to the view
10
[[self view] addGestureRecognizer:oneFingerTwoTaps];
11
 
12
消息方法oneFingerTwoTaps
13
- (void)oneFingerTwoTaps
14
{
15
  NSLog(@"Action: One finger, two taps");
16
}
[代碼] 兩個手指,拍擊兩次手勢
01
UITapGestureRecognizer *twoFingersTwoTaps =
02
  [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingersTwoTaps)] autorelease];
03
[twoFingersTwoTaps setNumberOfTapsRequired:2];
04
[twoFingersTwoTaps setNumberOfTouchesRequired:2];
05
[[self view] addGestureRecognizer:twoFingersTwoTaps];
06
 
07
消息方法twoFingersTwoTaps
08
- (void)twoFingersTwoTaps {
09
  NSLog(@"Action: Two fingers, two taps");
10
}
[代碼] 一個手指向上、向下擦碰手勢
01
// 向上擦碰
02
UISwipeGestureRecognizer *oneFingerSwipeUp =
03
  [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerSwipeUp:)] autorelease];
04
[oneFingerSwipeUp setDirection:UISwipeGestureRecognizerDirectionUp];
05
[[self view] addGestureRecognizer:oneFingerSwipeUp];
06
 
07
- (void)oneFingerSwipeUp:(UISwipeGestureRecognizer *)recognizer
08
{
09
  CGPoint point = [recognizer locationInView:[self view]];
10
  NSLog(@"Swipe up - start location: %f,%f", point.x, point.y);
11
}
12
 
13
// 向下擦碰
14
UISwipeGestureRecognizer *oneFingerSwipeDown =
15
  [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerSwipeDown:)] autorelease];
16
[oneFingerSwipeDown setDirection:UISwipeGestureRecognizerDirectionDown];
17
[[self view] addGestureRecognizer:oneFingerSwipeDown];
18
 
19
- (void)oneFingerSwipeDown:(UISwipeGestureRecognizer *)recognizer
20
{
21
  CGPoint point = [recognizer locationInView:[self view]];
22
  NSLog(@"Swipe down - start location: %f,%f", point.x, point.y);
23
}
[代碼] 旋轉手勢
1
UIRotationGestureRecognizer *twoFingersRotate =
2
  [[[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingersRotate:)] autorelease];
3
[[self view] addGestureRecognizer:twoFingersRotate];
4
 
5
- (void)twoFingersRotate:(UIRotationGestureRecognizer *)recognizer
6
{
7
  // Convert the radian value to show the degree of rotation
8
  NSLog(@"Rotation in degrees since last change: %f", [recognizer rotation] * (180 / M_PI));
9
}
[代碼] 向裡或向外捏的手勢
1
UIPinchGestureRecognizer *twoFingerPinch =
2
  [[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingerPinch:)] autorelease];
3
[[self view] addGestureRecognizer:twoFingerPinch];
4
 
5
- (void)twoFingerPinch:(UIPinchGestureRecognizer *)recognizer
6
{
7
  NSLog(@"Pinch scale: %f", recognizer.scale);
8
}

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved