将scrollViewWillEndDrag中的targetContentOffset更新为错误方向的值不会激活动画

Sun*_*kas 5 cocoa-touch uitableview uiscrollview uiscrollviewdelegate

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

但是,当沿速度的相反方向“向后”捕捉时,它会立即“向后捕捉”而没有动画。这看起来很糟糕。关于如何解决此问题的任何想法。

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
    }
}
Run Code Online (Sandbox Code Playgroud)

我可以有力地计算出何时会出现“错误”,并可能使用另一种“快照滚动”的方式。关于如何执行此操作或targetContentOffset.pointee.y正常解决问题的任何建议?

Sun*_*kas 1

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

我刚刚检测到何时出现不需要的“非动画快速滚动”,然后我没有将其设置为新值,而是将其设置为当前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)
    }
}
Run Code Online (Sandbox Code Playgroud)