【问题标题】:Swipe gesture recognizers not triggering method call滑动手势识别器不触发方法调用
【发布时间】:2013-10-08 22:42:22
【问题描述】:

我在 viewDidLoad 中添加了几个滑动手势识别器,但是没有调用指定的选择器:

UISwipeGestureRecognizer *swipeGestureRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleRightSwipe:)];
swipeGestureRight.numberOfTouchesRequired = 1;
swipeGestureRight.direction = (UISwipeGestureRecognizerDirectionRight);
[self.scrollView addGestureRecognizer:swipeGestureRight];

UISwipeGestureRecognizer *swipeGestureLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleLeftSwipe:)];
swipeGestureLeft.numberOfTouchesRequired = 1;
swipeGestureLeft.direction = (UISwipeGestureRecognizerDirectionLeft);
[self.scrollView addGestureRecognizer:swipeGestureLeft];

如果我将选择器更改为 @selector(crashAtRunTimeWithUndefinedMethod:) 则不会发生任何事情。 视图控制器继承自 UIGestureRecognizerDelegate。上述代码中没有设置手势识别器的委托,而是添加了它,并将其设置为 self 并没有任何区别。

scrollView 是控制器主视图的子视图,但我尝试将目标更改为主视图,这也没有区别。

【问题讨论】:

  • 你实现了哪些委托方法?
  • 我忘了实现 shouldRecognizeSimultaneouslyWithGestureRecognizer: 因为我也在使用平移手势识别器。我添加了它,它现在可以工作了。

标签: ios


【解决方案1】:

UIScrollView 已经具有预配置的手势识别器,可以检测滑动(和其他手势,如平移)。记录包含 UIScrollView 的手势识别的数组,您将看到它们。

并不是说你不能向 UIScrollView 添加额外的手势识别器,而是你必须小心。在我自己的应用程序中,我毫无问题地向 UIScrollView 添加了一个轻击手势识别器,因为它不与任何预配置的手势识别器竞争。

【讨论】:

    猜你喜欢
    • 2015-07-23
    • 1970-01-01
    • 2014-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多