【问题标题】:Add functionality to standard Pan Gesture Recognizer in a UIScrollView向 UIScrollView 中的标准平移手势识别器添加功能
【发布时间】:2012-01-12 17:14:30
【问题描述】:

我正在尝试跟踪手指在UIScrollView 中的位置。 我已将 UIScrollView 子类化(见下文),但不幸的是,我添加的手势识别器覆盖了标准的。

结果我得到了NSLog(@"Pan") 的工作,但不幸的是视图不再滚动。

如何让两个手势识别器同时工作?

谢谢。

- (void)viewDidLoad:(BOOL)animated
{
    [super viewDidLoad:animated];

    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    [scrollView addGestureRecognizer:panRecognizer];
}


- (void)pan:(id)sender {
    NSLog(@"Pan");
}

【问题讨论】:

  • 当两件事同时发生时,你不会说你期望发生什么。您希望它滚动更新您的平移识别器吗?如果是这样,为什么不只听滚动视图时调用的滚动视图委托方法?
  • 我希望视图能够滚动并记录所有触摸的点(我知道我可以通过locationInView: 方法检索)。滚动视图委托听起来很有趣 - 我从来没有听说过......我对 iOS 编程很陌生 - 那将如何工作?谢谢。
  • 我找到了this reference on scroll view delegates,但我不明白如何检索触摸的坐标。

标签: ios uiview uiscrollview uigesturerecognizer


【解决方案1】:

如果您不希望它覆盖标准的,您只需允许同时识别两者。

- (void)viewDidLoad:(BOOL)animated
{
    [super viewDidLoad:animated];

    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    panRecognizer.delegate = self;
    [scrollView addGestureRecognizer:panRecognizer];
}

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


- (void)pan:(id)sender {
    NSLog(@"Pan");
}

【讨论】:

    【解决方案2】:

    编辑:此方法有效!您只需尽快设置canCancelContentTouches(我在viewDidLoad 中设置)。

    原始答案:我尝试了一种新方法,但不幸的是它并不能完全奏效。

    我没有添加手势识别器,而是将UIScrollView 子类化并编写自己的touchesBegantouchesMoved 等方法。

    这样我就知道用户在哪里触摸,但不幸的是,每次我开始滚动时 PanGestureRecognizer 都会触发 touchesCancelled即使在将 canCancelContentTouches 设置为 NO 之后也是如此

    有人知道为什么吗?我也找到了this

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-10
      • 2013-09-28
      • 2019-03-27
      相关资源
      最近更新 更多