IOS: How to make a scrollview scroll only when touched outside of a subview?
<p>I have a scrollView with a subview inside of it. 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).</p>

<pre><code>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)
    }
}
</code></pre>

<hr>

<p>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:</p>

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

    override func touchesMoved(_ touches: Set&lt;UITouch&gt;, with event: UIEvent?) {
        self.isScrollEnabled = true
    }

    override func touchesEnded(_ touches: Set&lt;UITouch&gt;, with event: UIEvent?) {
        self.isScrollEnabled = true
    }
}
</code></pre>

<p>For the scrollview customclass I turned scrolling on and then for the subview custom class I turned it off. This works but there is a major problem that the first touch still goes ahead before the isScrollEnabled changes. 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.</p>

<p>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. </p>

<p>Cheers, any help is appreciated.</p>