Apple 提供了各種手勢識別器的類別。以下會用到輕按、輕掃、張合、旋轉(搖動暫不涉及)幾種手勢識別器,每個手勢都以彈出式視窗作為回饋。
在 ViewController.m 中:
1-點擊事件
-(void)foundTap:(UITapGestureRecognizer *)recognizer{
UIAlertView* alert=[[UIAlertView alloc] initWithTitle:@"Hello" message:@"tapped" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
- (void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer *tapRecognizer; //建立一個輕按手勢識別器
tapRecognizer=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(foundTap:)]; //初始化識別器,並以函式指標觸發對應方法
tapRecognizer.numberOfTapsRequired=1; //輕按次數(1 次)觸發
tapRecognizer.numberOfTouchesRequired=1; //要求響應的手指數
[self.view addGestureRecognizer:tapRecognizer]; //為物件加上手勢控制器
}
仔細觀察會發現,每個手勢的實作都遵循同樣步驟:建立控制器實例 → 實例初始化 → 加到類別物件 → 實作函式指標中的方法(@selector())。
【若想取得輕按或輕掃手勢的座標,可加上:CGPoint location=[recognizer locationInView:<the view>],其中 <the view> 即手勢識別器所在的視圖名;location 有 x 與 y 兩個參數。】
2-輕掃:
按上述步驟實作輕掃。函式指標指向的方法:
-(void)foundSwipe:(UISwipeGestureRecognizer *)recognizer{
UIAlertView* alert=[[UIAlertView alloc] initWithTitle:@"Hello" message:@"swiped" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
viewDidLoad 中:
UISwipeGestureRecognizer *swipeRecognizer; //建立輕掃識別器
swipeRecognizer=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(foundSwipe:)]; //初始化並設函式指標
swipeRecognizer.direction= UISwipeGestureRecognizerDirectionRight|UISwipeGestureRecognizerDirectionLeft; //向左或向右滑動
swipeRecognizer.numberOfTouchesRequired=1; //掃動手指數
[self.view addGestureRecognizer:swipeRecognizer]; //加上控制器
掃動方向有四種可選:UISwipeGestureRecognizerDirectionRight/Left/Up/Down。
3-張合(縮放):
函式指標方法:
-(void)foundPinch:(UIPinchGestureRecognizer *)recognizer{ //注意此處的類別
NSString *message;
double scale;
scale=recognizer.scale; //識別器的 scale(縮放比例)
//可用 scale 結合 openGLES 的 glScalef 實現縮放
message=[[NSString alloc]initWithFormat:@"縮放:Scale:%1.2f,Velocity:%1.2f",recognizer.scale,recognizer.velocity];
UIAlertView* alert=[[UIAlertView alloc] initWithTitle:@"Hello" message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
ViewDidLoad 中:
UIPinchGestureRecognizer *pinchReconizer; //建立張合識別器物件
pinchReconizer=[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(foundPinch)]; //初始化物件
[self.view addGestureRecognizer:pinchReconizer]; //加上控制器
4-旋轉
Cocoa 類別以弧度為單位,角度與弧度的換算關係為:角度 = 弧度 * 180 / Pi。
函式指標指向的方法:
-(void)foundRotation:(UIRotationGestureRecognizer *)recognizer{
NSString *message;
double rotation;
rotation=recognizer.rotation; //回傳控制器角度
//在 openGLES 中可用 rotation 結合 glRotatef 實現旋轉
message=[[NSString alloc] initWithFormat:@"旋轉,Radians:%1.2f,Velocity:%1.2f", recognizer.rotation,recognizer.velocity];
UIAlertView* alert=[[UIAlertView alloc] initWithTitle:@"Hello" message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
viewDidLoad 中:
UIRotationGestureRecognizer *rotationRecognizer; //建立旋轉手勢控制器物件
rotationRecognizer=[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(foundRotation:)]; //初始化物件
[self.view addGestureRecognizer:rotationRecognizer]; //加上控制器