我有以下UIGestureRecognizer
检测到正确滑动
- (void) addGestureRecognizer {
_swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)];
[_swipeRecognizer setDirection:UISwipeGestureRecognizerDirectionUp];
[_swipeRecognizer setDelegate:self];
[self.view addGestureRecognizer:_swipeRecognizer];
}
- (void) didSwipe:(id)sender{
NSLog(@"didSwipe");
}
然后我修改了方向以包括左和右
[_swipeRecognizer setDirection:UISwipeGestureRecognizerDirectionUp|UISwipeGestureRecognizerDirectionLeft|UISwipeGestureRecognizerDirectionRight];
它不再响应向上滑动(向左或向右滑动)。我究竟做错了什么? (试过模拟器,iphone5,ipad3)
注意:我不需要检测滑动的实际方向。我只需要知道有一个滑动。感谢。
答案 0 :(得分:1)
试试这种方式
UISwipeGestureRecognizer *_swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)];
[_swipeRecognizer setDirection:UISwipeGestureRecognizerDirectionLeft|UISwipeGestureRecognizerDirectionRight];
[_swipeRecognizer setDelegate:self];
[self.view addGestureRecognizer:_swipeRecognizer];
_swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)];
[_swipeRecognizer setDirection:UISwipeGestureRecognizerDirectionUp|UISwipeGestureRecognizerDirectionDown];
[_swipeRecognizer setDelegate:self];
[self.view addGestureRecognizer:_swipeRecognizer];
修改强>
As @ LearnCocos2D suggested“显然每个UISwipeGestureRecognizer都只能检测到给定方向的滑动。即使方向标志可以被”或“在一起,UISwipeGestureRecognizer也会忽略其他标志。 “
根据您的“注意:我不需要检测滑动的实际方向。我只需要知道有滑动。”,你只需要检测滑动而不是方向,因此将右 - 左和上 - 下组合,因为其他手势将起作用。
答案 1 :(得分:0)
swipeGesture是discree手势,上下滑动是两种不同的手势。 您必须创建4个手势并将其发送到一个动作。
- (void) addGestureRecognizer {
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)];
[swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[swipeLeft setDelegate:self];
[self.view addGestureRecognizer:swipeLeft];
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)];
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
[swipeRight setDelegate:self];
[self.view addGestureRecognizer:swipeRight];
}
- (void) didSwipe:(UISwipeGestureRecognizer *)sender{
NSLog(@"didSwipe");
}