【问题标题】:Updating targetContentOffset in scrollViewWillEndDragging to a value in wrong direction does not animate将 scrollViewWillEndDragging 中的 targetContentOffset 更新为错误方向的值不会设置动画
【发布时间】:2017-06-15 09:01:33
【问题描述】:

我正在为scrollViewWillEndDragging(_:withVelocity:targetContentOffset:) 中的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
    }
}

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

【问题讨论】:

  • 你的问题本身的标题拯救了我的一天。非常感谢你。我无法让自己在什么情况下动画会中断。就我而言,我将通过始终自动滚动到“正确”方向来修复它。再次感谢!

标签: uitableview cocoa-touch uiscrollview uiscrollviewdelegate


【解决方案1】:

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

我刚刚检测到何时会出现不需要的“非动画快照滚动”,然后我没有将新值设置为 targetContentOffset.pointee.y,而是将其设置为当前偏移值(停止它)并使用改为滚动视图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)
    }
}

【讨论】:

  • 你是如何检测到willSnapScrollBackWithoutAnimation的?
  • willSnapBackWithoutAnimation 设置为 true 当滚动处于可捕捉间隔(滚动的 yOffset)且速度低于所需值(使用 1.0)且速度不为 0 时。
  • @Sunkas 为什么不发布willSnapScrollBackWithoutAnimation 的实际代码而不是解释它?
  • 好问题。应该这样做的。不幸的是,您无法再访问代码了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-01
  • 1970-01-01
  • 2017-05-29
  • 1970-01-01
相关资源
最近更新 更多