简体   繁体   English

iOS:仅当在子视图外部触摸时,如何使滚动视图滚动?

[英]IOS: How to make a scrollview scroll only when touched outside of a subview?

I have a scrollView with a subview inside of it. 我有一个带有子视图的scrollView。 I want any touch outside of the subview to make the scrollview scroll like it would in any other circumstance. 我希望在子视图之外进行任何触摸,以使滚动视图像在任何其他情况下一样滚动。 But then when the touch is inside the subview I'd like no scroll to be registered (so I can use that touch to do dragging). 但是,当触摸位于子视图内部时,我不想注册任何滚动(因此我可以使用该触摸进行拖动)。

class ViewController: UIViewController {
    @IBOutlet weak var scrollView: UIScrollView!

    override func viewDidLoad() {
        super.viewDidLoad()
        scrollView.backgroundColor = UIColor.red
        scrollView.contentSize = CGSize(width: 1000, height: 1000)
        let view = UIView(frame: CGRect(x: 600, y: 600, width: 200, height: 200))
        view.backgroundColor = UIColor.blue
        scrollView.addSubview(view)
    }
}

So far I have tried many things to make this work. 到目前为止,我已经尝试了许多方法来使这项工作生效。 I have had some success with making customs classes for both the subview and the scrollview. 在为子视图和滚动视图创建海关类方面我取得了一些成功。 In these classes I override the touch methods like so: 在这些类中,我将重写touch方法,如下所示:

class MyScrollView: UIScrollView {
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.isScrollEnabled = true
    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.isScrollEnabled = true
    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.isScrollEnabled = true
    }
}

For the scrollview customclass I turned scrolling on and then for the subview custom class I turned it off. 对于scrollview自定义类,我打开了滚动,然后对于子视图自定义类,我关闭了滚动。 This works but there is a major problem that the first touch still goes ahead before the isScrollEnabled changes. 这可行,但是存在一个主要问题,即在isScrollEnabled更改之前,第一次触摸仍会继续进行。 This means when the scroll is off and you want to scroll you have to touch outside of the box and nothing happens and then do the same touch and the touch begins. 这意味着当滚动关闭并且要滚动时,您必须在框外触摸,什么也没有发生,然后再进行相同的触摸,触摸就会开始。

What I am looking for is a way to realise where a touch is, change the scroll settings and then on that same touch still scroll or not scroll dependent on what the setting is. 我正在寻找的一种方法是实现触摸的位置,更改滚动设置,然后在同一触摸上仍然滚动或不滚动,具体取决于设置是什么。

Cheers, any help is appreciated. 干杯,任何帮助表示赞赏。

Probably the easiest way to do it is to add a dummy UIPanGestureRecognizer to the blue view like this: 可能最简单的方法是向蓝色视图中添加一个伪UIPanGestureRecognizer ,如下所示:

blueView.addGestureRecognizer(UIPanGestureRecognizer())

Even though there is no action, this will do the job because a touch will be handled by the subview first and only then handled by UIScrollView 's own UIPagGestureRecognizer . 即使没有动作,也可以完成此工作,因为触摸将首先由子视图处理,然后才由UIScrollView自己的UIPagGestureRecognizer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM