【问题标题】:QML - Why do animations conflict?QML - 为什么动画会发生冲突?
【发布时间】:2018-02-17 09:00:45
【问题描述】:

Qml - Flipable 的示例:

import QtQuick 2.0

Flipable {
    id: flipable
    width: 240
    height: 240

    property bool flipped: false

    front: Rectangle { width: 200; height: 200; color: 'red'; anchors.centerIn: parent }
    back: Rectangle { width: 200; height: 200; color: 'blue'; anchors.centerIn: parent }

    transform: Rotation {
        id: rotation
        origin.x: flipable.width/2
        origin.y: flipable.height/2
        axis.x: 0; axis.y: 1; axis.z: 0     // set axis.y to 1 to rotate around y-axis
        angle: 0    // the default angle
    }

    states: State {
        name: "back"
        PropertyChanges { target: rotation; angle: 180 }
        when: flipable.flipped
    }

    transitions: Transition {
        NumberAnimation { target: rotation; property: "angle"; duration: 4000 }
    }

    MouseArea {
        anchors.fill: parent
        onClicked: flipable.flipped = !flipable.flipped
    }
}

此示例运行良好,但如果我使用此代码,Flipable 将无法运行:

MouseArea {
  anchors.fill: parent
  onClicked: {
    flipable.flipped = true;
    flipable.flipped = false;
  }
}

我认为当我第一次制作flipped 属性时动画冲突是true 然后是false。而我希望 flipable 打开然后关闭。

【问题讨论】:

    标签: qt animation qml


    【解决方案1】:

    问题是您在翻转动画开始之前将属性flipped 设置回false

    如果您想要完整的打开/关闭动画,则必须等待“打开”动画完成才能开始“关闭”动画:

    transitions: Transition {
        id: transition
        onRunningChanged: {
            if (!running && flipable.flipped) {
                flipable.flipped = false;
            }
        }
        NumberAnimation { target: rotation; property: "angle"; duration: 4000 }
    }
    
    MouseArea {
        anchors.fill: parent
        onClicked: {
            if (!transition.running) {
                flipable.flipped = true;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-04-07
      • 2021-01-09
      • 2021-08-28
      • 1970-01-01
      • 2019-09-20
      • 2015-06-14
      • 2016-12-16
      • 2018-02-26
      • 1970-01-01
      相关资源
      最近更新 更多