今天,我在用UISwipeGestureRecognizer滑动控制器的根view的时候,发现无论往哪个方向轻扫,打印swipeGestureRecognizer.direction的值都是3.
下面是我以前的代码:
UISwipeGestureRecognizer *swipeGestureRecognizer =[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
// 轻扫手势的direction默认就是UISwipeGestureRecognizerDirectionRight swipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeGestureRecognizer];
这是错误的。
正确的用法:要创建两个轻扫手势,分别设定其轻扫方向才是正确的。
// 添加向左轻扫手势
UISwipeGestureRecognizer *swipeGestureRecognizerLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
[self.view addGestureRecognizer:swipeGestureRecognizerLeft];
swipeGestureRecognizerLeft.direction = UISwipeGestureRecognizerDirectionLeft;
// 添加向右轻扫手势
UISwipeGestureRecognizer *swipeGestureRecognizerRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
[self.view addGestureRecognizer:swipeGestureRecognizerRight];
swipeGestureRecognizerRight.direction = UISwipeGestureRecognizerDirectionRight;
// 监听手势的方法
- (void)swip:(UISwipeGestureRecognizer *)sender {
NSLog(@"%ld", sender.direction); // 判断轻扫手势的方向
}