【问题标题】:iOS - UIScrollView hitTest doesn't contain touchesiOS - UIScrollView hitTest 不包含触摸
【发布时间】:2014-05-21 17:39:57
【问题描述】:

我有一个 UIScrollView,里面有 UIViews。 UIScrollView 启用了两指滚动。每个 UIView 都有一个 panGestureRecognizer。我想要以下功能:

如果是两点触控平移 --> 滚动。

如果在 UIView 上单击 pan && touch --> 触发 UIView 的 panGestureRecognizer。

我想通过覆盖 UIScrollView 的 hitTest 来做到这一点。如果触摸次数大于 1,则返回 UIScrollView 以可能滚动。如果触摸次数为 1,则返回正常的 hitTest 结果以可能触发 UIView 的 panGestureRecognizer。但是我的 UIScrollView 的 hitTest 代码从来没有任何接触! (虽然我成功两指滚动,但hitTest没有任何触摸。)

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    NSSet *touches = [event touchesForView:self];
    NSLog(@"%@", touches);
    if ([touches count] > 1)
    {
        return self;
    }
    UIView *usualView = [super hitTest:point withEvent:event];
    return usualView;
}

【问题讨论】:

  • 您的用户不会理解这一点。并且用户不阅读教程。
  • 这是一堆可以滚动的画布上的音符。我认为它实际上非常直观:拖放音符。两指滚动浏览歌曲。

标签: ios uiscrollview uigesturerecognizer hittest uievent


【解决方案1】:

HitTest 是一种用于处理触摸的低级可重写方法,或者更好地说,用于检测触摸的目的地。您无法知道此处的触摸次数 - event 参数无用。相反,每次触摸都会调用两次,这意味着两次触摸会调用 4 次。 它不适用于检测内部的多次触摸或手势,仅用于触摸的目的地。

默认实现类似于:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    if (self.hidden || !self.userInteractionEnabled || self.alpha < 0.01 || ![self pointInside:point withEvent:event] || ![self _isAnimatedUserInteractionEnabled]) {
        return nil;
    } else {
        for (UIView *subview in [self.subviews reverseObjectEnumerator]) {
            UIView *hitView = [subview hitTest:[subview convertPoint:point fromView:self] withEvent:event];
            if (hitView) {
                return hitView;
            }
        }
        return self;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 2013-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-28
    • 1970-01-01
    相关资源
    最近更新 更多