我正在使用一个表盘,当用户触摸它并将其拖动时,该表盘会旋转。到目前为止一切都很好,但是当表盘达到360以上时,该值会回到0,使动画在表盘周围向后跳而不是继续。
dialRotation = (atan2(event->localY()-circleCenterY, event->localX()-circleCenterX) * (180/M_PI));
有谁知道如何阻止它跳跃?
答案 0 :(得分:0)
您可以使用现有值来确定是否应超过360。也许是这样的:
currentValue = dialRotation;
dialRotation = (atan2(event->localY()-circleCenterY, event->localX()-circleCenterX) * (180/M_PI));
dialRotation = 360.0 * floor(fmod(currentValue, 360.0)) + dialRotation;
我认为这也会向负面方向发挥作用,尽管我有时会对fmod()
的负数行为感到困惑,所以一定要检查它。
答案 1 :(得分:0)
另一种方法是获取前一个值和当前值之间的增量(变化),然后将其添加到您已有的值。只要差异不大于180度,这可能会更好。像这样:
// In your class declaration:
float normalizedRotation; // Always between 0 and 360 degrees
float previousNormalizedRotation;
float dialRotation; // current value, can be any valid value from -inf to +inf
// In your method:
normalizedRotation = (atan2(event->localY()-circleCenterY, event->localX()-circleCenterX) * (180/M_PI));
if (normalizedRotation < 0.0) normalizedRotation += 360.0;
float delta = normalizedRotation - previousNormalizedRotation;
previousNormalizedRotation = normalizedRotation;
dialRotation += delta;
如果有效,请告诉我。