How to get notified when scrollToRowAtIndexPath finishes animating

前端 未结 6 1870
梦如初夏
梦如初夏 2020-11-30 09:57

This is a follow-up to How to get notified when a tableViewController finishes animating the push onto a nav stack.

In a tableView I want to deselect a

6条回答
  •  我在风中等你
    2020-11-30 10:06

    Swift 5

    The scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) delegate method is indeed the best way to execute a completion on a scroll-to-row animation but there are two things worth noting:

    First, the documentation incorrectly says that this method is only called in response to setContentOffset and scrollRectToVisible; it's also called in response to scrollToRow (https://developer.apple.com/documentation/uikit/uiscrollviewdelegate/1619379-scrollviewdidendscrollinganimati).

    Second, despite the fact that the method is called on the main thread, if you're running a subsequent animation here (one after the scroll has finished), it will still hitch (this may or may not be a bug in UIKit). Therefore, simply dispatch any follow-up animations back onto the main queue which just ensures that the animations will begin after the end of the current main task (which appears to include the scroll-to-row animation). Doing this will give you the appearance of a true completion.

    func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
        DispatchQueue.main.async {
            // execute subsequent animation here
        }
    }
    

提交回复
热议问题