Search code examples
iosswiftuiscrollviewuiscrollviewdelegate

UIScrollView touch doesnt work when Auto Scroll


I have a UIScrollView inside which I have several items. I have a feature in my app if someone presses a button scroll view starts scrolling automatically at a certain speed. But that causes all the touches to stop working in scroll view. My goal is to whenever a user starts scrolling manually I want the auto-scrolling to get stopped automatically. But manual scrolling stops working when auto-scrolling is in running.

Here is my codes for auto-scrolling

self.scrollTimer = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(timerAction), userInfo: nil, repeats: true)
    self.scrollTimer!.fire()
    RunLoop.current.add(self.scrollTimer!, forMode: RunLoopMode.commonModes)

var yOffset: CGFloat = 0
    var offsetIncrementer: CGFloat = 5

    @objc func timerAction() {
        yOffset += offsetIncrementer
        if quranContainer.frame.height <= yOffset + self.view.frame.height {
            yOffset = 0
        }

        DispatchQueue.main.async {
            UIView.animate(withDuration: 1.0) {
                self.scrollView.contentOffset.y = self.yOffset
            }
        }
    }

My Output https://youtu.be/QrYovPMvoBo


Solution

  • Okay solved the issue. UIView animation was removing the userInteractions. Needed to pass allowUserInteraction in options with the animate method

    This is how I solved the issue:

    UIView.animate(withDuration: 1.0,
                               delay: 0,
                               options: [.allowUserInteraction],
                               animations: {
                                self.scrollView.contentOffset.y = self.yOffset
                })