使 UIScrollView 仅适用于内容部分,而不是“侧面" [英] Make UIScrollView work only on content part, not "sides"

查看:22
本文介绍了使 UIScrollView 仅适用于内容部分,而不是“侧面"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个滚动视图(它是灰色区域).内容区域为黄色.请注意它的插入,以便您可以将内容的两侧滚动到屏幕中间.

Here's a scroll view (it is the GRAY area). Content area is yellow. Notice it insets, so that you can scroll the sides of the content over to the middle of the screen.

问题:您可以将手指放在灰色区域并滚动它.

PROBLEM: you can put your finger on the GRAY area and scroll it.

我希望它能够工作,以便您只能通过将手指放在黄色内容上来滚动.如果您将手指放在灰色的背景"上并尝试滚动它,则不会发生任何事情.

I want it to work so that you can ONLY scroll by putting your finger on the yellow content. If you put your finger on the gray "background" and try to scroll it, nothing should happen.

如何做到这一点?

黄色内容完全向右滚动,它停在这里:

Yellow content is scrolled fully to the right, it rests here:

黄色内容完全向左滚动,它停在这里:

Yellow content is scrolled fully to the left, it rests here:

推荐答案

创建 UIScrollView 的子类.给它一个对黄色视图的引用.覆盖 pointInside:withEvent: 以返回 false 除非点在黄色视图的框架中.

Make a subclass of UIScrollView. Give it a reference to the yellow view. Override pointInside:withEvent: to return false unless the point is in the frame of the yellow view.

class MyScrollView: UIScrollView {

    @IBOutlet var yellowView: UIView?

    override func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool {
        return yellowView?.frame.contains(point) ?? false
    }

}

这篇关于使 UIScrollView 仅适用于内容部分,而不是“侧面"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆