UIScrollView touch doesnt work when Auto Scroll

171 views Asked by At

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

2

There are 2 answers

0
Ahsan Aasim On BEST ANSWER

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
            })
4
dengApro On
 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
         super.touchesBegan(touches, with: event)
         stopPlayerTimer() 
  }


  override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?){
        super.touchesEnded(touches, with: event)
        startPlayerTimer()
   }


    func startPlayerTimer(){
        if self.scrollTimer == nil {
            Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(timerAction), userInfo: nil, repeats: true)
            self.scrollTimer?.fire()
        }
    }

    func stopPlayerTimer(){
        self.scrollTimer?.invalidate()
        self.scrollTimer = nil
     }


schedule, then fire.

self.scrollTimer = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(timerAction), userInfo: nil, repeats: true)
self.scrollTimer.fire()

Because the scrolling disabled your timer's updating.

Your timer is not under RunLoop.Mode.tracking by default


add the timer to the common mode of RunLoop, then ok

RunLoop.current.add(timer, forMode: RunLoop.Mode.common)