具有“速度”和“速度”的 QML 动画和无限“循环”

发布于 2024-11-04 09:46:39 字数 1180 浏览 0 评论 0原文

我正在尝试制作一个动画,在其中指定速度(而不是持续时间)并且永远循环。我想出了两个不起作用的示例:

FirstTry.qml

import Qt 4.7

Rectangle {
  width: 100; height: 100
  Text {
    text: "hello"
    NumberAnimation on x {
      to: 50;
      loops: Animation.Infinite;
      duration: 50 * Math.abs(to - from)
    }
  }
}

hello 在屏幕上发疯时,我收到以下运行时警告(这很公平)。

QDeclarativeExpression: Expression "(function() { return 50 * Math.abs(to - from) })" depends on non-NOTIFYable properties: 
    QDeclarativeNumberAnimation::to
    QDeclarativeNumberAnimation::from

SecondTry.qml

import Qt 4.7

Rectangle {
  width: 100; height: 100
  Text {
    text: "hello"
    SmoothedAnimation on x {
      to: 50;
      loops: Animation.Infinite;
      velocity: 50
    }
  }
}

这更加神秘——SmoothedAnimation 根本拒绝循环!动画运行一次,然后就这样了。

所以我有以下问题:

是否有合法的方法来指定第一个示例中的速度?我知道 SmoothedAnimation 是从 NumberAnimation 派生的,所以也许它可以在 QML 中实现,而不仅仅是在 C++ 中。

有没有办法让 SmoothedAnimation 循环?第二个示例不起作用,是错误还是我遗漏了某些内容?

有没有其他方法可以同时实现这两种行为?

I'm trying to put together an animation in which I get to specify the velocity (rather than the duration) and which loops forever. I came up with two non-working examples:

FirstTry.qml

import Qt 4.7

Rectangle {
  width: 100; height: 100
  Text {
    text: "hello"
    NumberAnimation on x {
      to: 50;
      loops: Animation.Infinite;
      duration: 50 * Math.abs(to - from)
    }
  }
}

I get the following runtime warning while hello goes nuts on the screen (fair enough).

QDeclarativeExpression: Expression "(function() { return 50 * Math.abs(to - from) })" depends on non-NOTIFYable properties: 
    QDeclarativeNumberAnimation::to
    QDeclarativeNumberAnimation::from

SecondTry.qml

import Qt 4.7

Rectangle {
  width: 100; height: 100
  Text {
    text: "hello"
    SmoothedAnimation on x {
      to: 50;
      loops: Animation.Infinite;
      velocity: 50
    }
  }
}

This is more of a mistery -- SmoothedAnimation simply refuses to loop! The Animation runs once and then that's it.

So I have the following questions:

Is there a legal way to specify the velocity in the first example? I understand SmoothedAnimation is derived from NumberAnimation, so maybe it's possible in QML, not just in C++.

Is there a way to make SmoothedAnimation loop? Is the second example not working a bug or am I missing something?

Is there any other way to achieve these two behaviours at the same time?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

Oo萌小芽oO 2024-11-11 09:46:40

只需显式添加“from”参数:

import Qt 4.7

Rectangle {
  width: 100; height: 100
  Text {
    text: "hello"
    NumberAnimation on x {
      from: 0;
      to: 50;
      loops: Animation.Infinite;
      duration: 50 * Math.abs(to - from)
    }
  }
}

just add "from" parameter explicitly:

import Qt 4.7

Rectangle {
  width: 100; height: 100
  Text {
    text: "hello"
    NumberAnimation on x {
      from: 0;
      to: 50;
      loops: Animation.Infinite;
      duration: 50 * Math.abs(to - from)
    }
  }
}
梦初启 2024-11-11 09:46:40

这是我作为临时解决方案所做的,我不确定它是否足够,但它似乎正在做我需要的事情。

SmoothedAnimation on x {
  to: 50;
  //loops: Animation.Infinite;
  velocity: 50
  onComplete: { restart (); }
}

不过,我仍然对问题的答案感兴趣。

This is what I did as a temporary solution, I'm not sure if it's adequate, but it seem to be doing just what I needed.

SmoothedAnimation on x {
  to: 50;
  //loops: Animation.Infinite;
  velocity: 50
  onComplete: { restart (); }
}

I'm still interested in the answers to the questions, though.

东走西顾 2024-11-11 09:46:40

即使SmoothedAnimation不接受loops参数,您也可以将其放置在SequentialAnimation内并将loops应用于此外部覆盖。作为效果,您的平滑动画将连续播放。

Even if SmoothedAnimation doesn't accept loops parameter, you can place it inside SequentialAnimation and apply loops to this external cover. As an effect your smoothed animation will be played continuosly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文