在viewDidLoad()的方法中,第一次启动就加入手势控制的代码:
//通过用户手势来处理
UISwipeGestureRecognizer *recognizer;
recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[self.view addGestureRecognizer:recognizer];
[recognizer release];
在方法handleSwipFrom这个方法捕获了手势之后,处理一定的逻辑,我这里主要是弹出当前的界面
-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer{
if (recognizer.direction == UISwipeGestureRecognizerDirectionRight) {
[self.navigationController popViewControllerAnimated:YES];
}
}
手势功能比较健壮,可以通过上下左右四个方向来控制手势,捕获手势。