【问题标题】:How to predict a good position for snapping a flickable in QML如何预测在 QML 中捕捉 flickable 的好位置
【发布时间】:2016-03-02 00:13:45
【问题描述】:

我有一个 flickable,我想捕捉到某些位置(我以特定方式计算)。我看到两个选项:

  1. 在 motionEnded 中,我使用数字动画捕捉到离“contentX”最近的位置
  2. 在 flickEnded 中,我使用数字动画捕捉到离“contentX”最近的位置

选项 1 看起来不太好,移动停止,然后轻弹再次开始移动(捕捉位置相隔半个屏幕)。

在选项 2 中,我发现捕捉位置的选择很困难。如果用户快速移动滑动,我更愿意捕捉到接近移动结束位置的位置,但这很难预测。

这是选项 1 的示例代码:

NumberAnimation on contentY {
        id: yAnimation
        to: 0
        duration: 200
    }
onMovementEnded: {
       var index = Math.round(contentY / (itemHeight))
       yAnimation.to = index * itemHeight
       yAnimation.start()
}

【问题讨论】:

  • 你的代码是什么样的?听起来你知道你需要做什么(选项 2),但你还没有正确的公式。如果您向我们展示您如何尝试预测运动,我们可以为您提供帮助。与往常一样,一个最小的、完整的例子意味着你会得到更多的答案。

标签: qt qml flickable


【解决方案1】:

在选项 2 中,我发现捕捉位置的选择很困难。如果用户 快速移动的幻灯片,我更喜欢捕捉到一个位置 接近运动结束的位置,但这很难 预测。

你有水平和垂直速度的属性,所以你可以计算复合速度,还有另一个

property bool aboutToStop: horizontalVelocity + verticalVelocity < thresholdValue

onAboutToStopChanged: if (aboutToStop) playASnapAnimationToTheNearestItem()

【讨论】:

  • 速度可能是负数,所以你应该使用readonly property bool aboutToStop: Math.abs(horizontalVelocity) + Math.abs(verticalVelocity) < aboutToStopThreshold
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-19
  • 1970-01-01
  • 2018-08-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多