How to know exactly when a UIScrollView's scrolling has stopped?

后端 未结 6 1035
慢半拍i
慢半拍i 2020-12-04 22:07

In short, I need to know exactly when the scrollview stopped scrolling. By \'stopped scrolling\', I mean the moment at which it is no longer moving and not being touched.

6条回答
  •  我在风中等你
    2020-12-04 22:33

    It is important to understand, that the when UIScrollView is stopping to move it triggers two different functions of the UIScrollViewDelegate depending on how strong the user has pushed.

    1. stopDragging is triggered, when the user stops his drag gesture on the screen. When this was very slowly no more movement will happen after that.
    2. stopDecelerating is triggered, when the user has pushed the scroll view in a way, so that it is still moving after the user touches up his finger and then comes to an end. This function is not always triggered, it is not triggered, when there is no movement after the user touches up his finger. Then only the function above "stopDragging" is triggered.

    So in conclusion it is necessary to combine the two functions like @WayneBurkett already pointed out in his answer.

    In Swift 5.X:

    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        myFunction()
    }
    
    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        if decelerate == false {
            myFunction()
        }
    }
    
    private func myFunction() {
        // scrollView did stop moving -> do what ever
        // ...
    }
    

提交回复
热议问题