Triggers a handler at every animation frame update. 更多...
import 语句: | import QtQuick |
Since: | Qt 6.4 |
A FrameAnimation can be used to trigger an action every time animations have progressed and an animation frame has been rendered. See the documentation about the 场景图形 for in-depth information about the threaded and basic render loops.
For general animations, prefer using
NumberAnimation
and other
动画
elements as those provide declarative way to describe the animations.
FrameAnimation on the other hand should be used for custom imperative animations and in use-cases like these:
Compared to
Timer
which allows to set the
interval
time, FrameAnimation runs always in synchronization with the animation updates. If you have used
Timer
with a short interval for custom animations like below, please consider switching to use FrameAnimation instead for smoother animations.
// BAD Timer { interval: 16 repeat: true running: true onTriggered: { // Animate something } } // GOOD FrameAnimation { running: true onTriggered: { // Animate something } }
currentFrame
:
int
|
This property holds the number of frame updates since the start. When the frame animation is restarted, currentFrame starts from
0
.
The following example shows how to react on frame updates.
FrameAnimation { running: true onTriggered: { // Run code on every frame update. } }
This property can also be used for rendering only every nth frame. Consider an advanced usage where the UI contains two heavy elements and to reach smooth 60fps overall frame rate, you decide to render these heavy elements at 30fps, first one on every even frames and second one on every odd frames:
FrameAnimation { running: true onTriggered: { if (currentFrame % 2 == 0) updateUIElement1(); else updateUIElement2(); } }
默认情况下,
frame
为 0。
elapsedTime
:
qreal
|
This property holds the time (in seconds) since the previous start.
默认情况下,
elapsedTime
为 0。
frameTime
:
qreal
|
This property holds the time (in seconds) since the previous frame update.
The following example shows how to use frameTime to animate item with varying speed, adjusting to screen refresh rates and possible fps drops.
Rectangle { id: rect property real speed: 90 width: 100 height: 100 color: "red" anchors.centerIn: parent } FrameAnimation { id: frameAnimation running: true onTriggered: { // Rotate the item speed-degrees / second. rect.rotation += rect.speed * frameTime } }
默认情况下,
frameTime
为 0。
paused : bool |
If set to true, pauses the frame animation; otherwise resumes it.
paused defaults to false.
running : bool |
If set to true, starts the frame animation; otherwise stops it.
running defaults to false.
另请参阅 stop (), start (),和 restart ().
smoothFrameTime
:
qreal
|
This property holds the smoothed time (in seconds) since the previous frame update.
The following example shows how to use smoothFrameTime to show average fps.
Text { text: "fps: " + frameAnimation.fps.toFixed(0) } FrameAnimation { id: frameAnimation property real fps: smoothFrameTime > 0 ? (1.0 / smoothFrameTime) : 0 running: true }
默认情况下,
smoothFrameTime
为 0。
triggered () |
此信号发射当 FrameAnimation has progressed to a new frame.
注意:
相应处理程序是
onTriggered
.
pause () |
Pauses the frame animation
If the frame animation is already paused or not
running
, calling this method has no effect. The
paused
property will be true following a call to
pause()
.
reset () |
Resets the frame animation properties
Calling this method resets the
frame
and
elapsedTime
to their initial values (0). This method has no effect on
running
or
paused
properties and can be called while they are true or false.
The difference between calling
reset()
and
restart()
is that
reset()
will always initialize the properties while
restart()
initializes them only at the next frame update which doesn't happen e.g. if
restart()
is immediately followed by
pause()
.
restart () |
Restarts the frame animation
若
FrameAnimation
is not running it will be started, otherwise it will be stopped, reset to initial state and started. The
running
property will be true following a call to
restart()
.
resume () |
Resumes a paused frame animation
If the frame animation is not paused or not
running
, calling this method has no effect. The
paused
property will be false following a call to
resume()
.
start () |
Starts the frame animation
If the frame animation is already running, calling this method has no effect. The
running
property will be true following a call to
start()
.
stop () |
Stops the frame animation
If the frame animation is not running, calling this method has no effect. Both the
running
and
paused
properties will be false following a call to
stop()
.