iOS - UIScrollView hitTest 不包含触摸

标签 ios uiscrollview uigesturerecognizer hittest uievent

我有一个 UIScrollView,其中包含 UIView。 UIScrollView 启用了双指滚动。每个 UIView 都有一个 panGestureRecognizer。我想要以下功能:

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

如果单点触摸 pan && 触摸 UIView --> 触发 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;
}

最佳答案

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;
    }
}

关于iOS - UIScrollView hitTest 不包含触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23790283/

相关文章:

ios - EKCalendarEKCalendar CGcolor未在iPhone日历中设置颜色

ios - cocos2d v3中如何停止CCScrollView对象的滚动动画

android - 移动网站在横向模式下的 Safari 上无法缩放

iphone - UITapGestureRecognizer 的问题

objective-c - 当 Action 的发送者是 UIGestureRecognizer 时引用按钮

ios - 使用 CH CSVParser 解析引用的 CSV

ios - 在 UIScrollView 上平移对象

iphone - 横向模式下的 UIScrollView 框架大小问题

ios - 使用 scrollViewDidScroll 移动 ChildViewController 的 subview

ios - 在 UITableViewCell 上禁用仅向左滑动。