// 宣告一個 recognizer, 並加到需要偵測該手勢的 UIView 元件上 - (void)viewDidLoad { UISwipeGestureRecognizer* recognizer; // handleSwipeFrom 是偵測到手勢後,所要呼叫的方法 recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom)]; // 不同的 Recognizer 有不同的實體變數 // 例如 SwipeGesture 可以指定方向 // 而 TapGesture 則可以指定次數 recognizer.direction = UISwipeGestureRecognizerDirectionUp [self.view addGestureRecognizer:recognizer]; [recognizer release]; } - (void)handleSwipeFrom:(UISwipeGestureRecognizer*)recognizer { // 觸發手勢事件後,在這裡作些事情 // 底下是刪除手勢的方式 [self.view removeGestureRecognizer:recognizer]; }