苹果开发。如何扩展UIScrollView的滚动事件响应区域?



我启用了UIScrollView的分页,并设置了它的clipToBounds = NO,以便我可以看到当前页面的外部

我想要的是使用户能够滚动UIScrollView在其整个可见区域,而不仅仅是当前页面的一部分。

我如何实现这个?

注:根据苹果的文档(https://developer.apple.com/documentation/uikit/uiscrollview),如果pagingEnabled属性的值是YES,当用户滚动时,滚动视图停止在滚动视图边界的倍数上。

这意味着我不能仅仅调整UIScrollView的大小来获得我想要的而不影响分页功能,对吗?

将scrollview放入uiview中并禁用scrollview的clipsToBounds。滚动视图必须是您想要的分页大小。您将在滚动视图之外看到内容,但分页仍然是相同的。您可以为外部视图激活clipsToBounce,以限制scrollview的大小。

重写视图的hitTest以将触摸重定向到滚动视图…然后你也可以通过触摸scrollview的外部来滚动

为什么不调整scrollView的大小,让它适合你需要的区域呢?

不确定是否有帮助,但尝试覆盖UIScrollView pointInside方法

- (BOOL) pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    CGRect newBounds = self.bounds;
    newBounds = CGRectInset(bounds, 0, -100.0f);
    return CGRectContainsPoint(newBounds, point);
}

2019语法示例:

class LargerScrollArea: UICollectionView {
    // Example, say this is the left half of the screen, but, we wish to be
    // able to scroll on the whole width of the screen
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        if isUserInteractionEnabled == false { return nil }
        let extraOnRight = bounds.width
        let largerBounds = bounds.inset(by:
                UIEdgeInsets(top: 0, left: 0, bottom:0, right: -extraOnRight))
        if largerBounds.contains(point) {
            return self
        }
        else {
            return nil
        }
    }
}

(集合视图,表视图,都一样)

最新更新