将 scrollViewWillEndDragging 中的 targetContentOffset 更新为错误方向的值不会设置动画

Updating targetContentOffset in scrollViewWillEndDragging to a value in wrong direction does not animate

我在 scrollViewWillEndDragging(_:withVelocity:targetContentOffset:) 中将新值设置为 targetContentOffset 以在 UITableView 中创建自定义分页解决方案。当为 targetContentOffset 设置与速度指向相同的滚动方向的坐标时,它会按预期工作。

但是,当以与速度相反的方向捕捉 "backward" 时,它会立即 "snap back" 而没有动画。这看起来很糟糕。关于如何解决这个问题的任何想法。

func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
    // a lot of caluculations to detemine where to "snap scroll" to
    if shouldSnapScrollUpFromSpeed || shouldSnapScrollUpFromDistance {
        targetContentOffset.pointee.y = -scrollView.frame.height
    } else if shouldSnapScrollDownFromSpeed || shouldSnapScrollDownFromDistance {
        targetContentOffset.pointee.y = -detailViewHeaderHeight
    }
}

我可以潜在地计算这个 "bug" 何时会出现,也许使用另一种方式 "snap scrolling"。关于如何使用 targetContentOffset.pointee.y 正常执行此操作或解决它的任何建议?

我找到了一个好的解决方案(或解决方法)。如果有人有更好的解决方案,请告诉我。

我只是检测到不需要的 "non-animated snap scroll" 会出现,然后我没有将新值设置为 targetContentOffset.pointee.y,而是将其设置为当前偏移值(停止它)并设置所需的偏移目标scrollViews 的值 setContentOffset(_:animated:) 而不是

if willSnapScrollBackWithoutAnimation {
    targetContentOffset.pointee.y = -scrollView.frame.height+yOffset //Stop the scrolling
    shouldSetTargetYOffsetDirectly = false
}

if let newTargetYOffset = newTargetYOffset {
    if shouldSetTargetYOffsetDirectly {
        targetContentOffset.pointee.y = newTargetYOffset
    } else {
        var newContentOffset = scrollView.contentOffset
        newContentOffset.y = newTargetYOffset
        scrollView.setContentOffset(newContentOffset, animated: true)
    }
}