iphone开发:同时启用手势识别滚动

时间:2012-09-03 13:06:39

标签: iphone objective-c ios scrollview gesture-recognition

在我的应用程序中,我需要在我的背景滚动条上有向上方向的滑动手势识别器。这是我的代码

它在viewDidLoad

UISwipeGestureRecognizer *Swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(SwipeRecognizer:)];
Swipe.direction = UISwipeGestureRecognizerDirectionUp;
[backgroundScroller addGestureRecognizer:Swipe];

它是SwipeRecognizer:

- (void) SwipeRecognizer:(UISwipeGestureRecognizer *)sender {
    if (sender.direction | UISwipeGestureRecognizerDirectionUp){
        NSLog(@" *** SWIPE UP ***");
    }
}

问题是我无法同时启用滚动和捕捉手势。当我说滚动没有启用时,我可以识别出手势。但我需要同时滚动和手势识别。是不是可能?

2 个答案:

答案 0 :(得分:2)

重写gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:方法,不要阻止UIScrollViews泛识别器

它会起作用......

不要忘记为手势识别器添加委托给自己。正如@ death7eater的评论中提到的那样。

答案 1 :(得分:1)

我解决了我的问题:

这是针对viewDidLoad:

UISwipeGestureRecognizer *Swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(SwipeRecognizer:)];
    Swipe.direction = UISwipeGestureRecognizerDirectionUp;
    [backgroundScroller addGestureRecognizer:Swipe];
    Swipe.delegate = self;

这是SwipeRecognizer方法:

- (void) SwipeRecognizer:(UISwipeGestureRecognizer *)sender {
    if (sender.direction | UISwipeGestureRecognizerDirectionUp){
        NSLog(@" *** SWIPE UP ***");
    }
}

感谢@lukaswelte,这允许同时执行多个手势:

- (BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}