在Xcode的IB中,添加一個手勢識別器和添加一個任何一個對象到界面上方式相同——從對象庫中拖拽一個手勢識別器到一個視圖上。你做完這些以后,手勢識別器會自動地綁定到這個視圖上。你可以檢查你的手勢識別器綁定到了哪個視圖,并且,如果必要的話你可以改變nib文件中的連接。
創(chuàng)建完手勢識別器對象之后,你需要創(chuàng)建并連接一個動作方法。這個方法將在連接的手勢識別器識別出它的手勢時被調(diào)用。如果你需要在這個動作方法之外引用這個手勢識別器,你應(yīng)該為這個手勢識別器再創(chuàng)建并連接一個接口。你的代碼應(yīng)和清單1-1很像。
Listing 1-1 Adding a gesture recognizer to your app with Interface Builder
[email protected] APLGestureRecognizerViewController ()
[email protected] (nonatomic, strong) IBOutlet UITapGestureRecognizer *tapRecognizer;
[email protected]
[email protected]
- (IBAction)displayGestureForTapRecognizer:(UITapGestureRecognizer *)recognizer
// Will implement method later...
}
[email protected]
使用代碼添加手勢識別器
你也可以通過alloc和init一個具體的UIGestureRecognizer子類(例如UIPinchGestureRecognizer)的實例。當(dāng)你初始化手勢識別器的時候,得指定一個目標(biāo)對象和一個動作選擇器(selector),像清單1-2那樣。目標(biāo)對象常常是視圖的視圖控制器。
如果通過代碼建立一個手勢識別器,你需要使用addGestureRecognizer: 方法把它綁定到視圖上。清單1-2創(chuàng)建了一個獨立的點擊手勢識別器,指定了需要識別的手勢是一次點擊,然后把手勢識別器對象綁定到了一個視圖上。典型的做法是,你在視圖控制器的viewDidLoad方法中創(chuàng)建手勢識別器,如清單1-2展示。
Listing 1-2 Creating a single tap gesture recognizer programmatically
- (void)viewDidLoad {
[super viewDidLoad];
// Create and initialize a tap gesture
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(respondToTapGesture:)];
// Specify that the gesture must be a single tap
tapRecognizer.numberOfTapsRequired = 1;
// Add the tap gesture recognizer to the view
[self.view addGestureRecognizer:tapRecognizer];
// Do any additional setup after loading the view, typically from a nib
}
對離散手勢的響應(yīng)
你在創(chuàng)建一個手勢識別器的時候會把識別器連接到一個動作方法上。使用這個方法對你的手勢識別器進行響應(yīng)。清單1-3提供一個對離散手勢進行響應(yīng)的例子。當(dāng)用戶點擊手勢識別器綁定的視圖時,視圖控制器顯示一張寫有“Tap.”的圖片。showGestureForTapRecognizer:方法會確定視圖上手勢的位置,這個位置信息來自識別器的locationInView屬性,然后把圖片在這個位置上顯示出來。
Note:下面的三個代碼案例都來自于Simple Gesture Recognizers案例項目,你可以查看更多的上下文。
Listing 1-3 Handling a double tap gesture
- (IBAction)showGestureForTapRecognizer:(UITapGestureRecognizer *)recognizer {
// Get the location of the gesture
CGPoint location = [recognizer locationInView:self.view];
// Display an image view at that location
[self drawImageForGestureRecognizer:recognizer atPoint:location];
// Animate the image view so that it fades out
[UIView animateWithDuration:0.5 animations:^{
self.imageView.alpha = 0.0;
}];
}
每一個手勢識別器都有自己的屬性集合。例如,在清單1-4中,showGestureForSwipeRecognizer:方法使用了swipe手勢識別器的方向?qū)傩詠泶_定用戶是往左滑還是往右滑。然后,它使用這個值來使圖片從滑動方向逐漸消失掉。
Listing 1-4 Responding to a left or right swipe gesture
// Respond to a swipe gesture
- (IBAction)showGestureForSwipeRecognizer:(UISwipeGestureRecognizer *)recognizer {
// Get the location of the gesture
CGPoint location = [recognizer locationInView:self.view];
// Display an image view at that location
[self drawImageForGestureRecognizer:recognizer atPoint:location];
// If gesture is a left swipe, specify an end location
// to the left of the current location
if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft) {
location.x -= 220.0;
} else {
location.x += 220.0;
}
// Animate the image view in the direction of the swipe as it fades out