【问题标题】:QML RotationAnimation is not rotatingQML RotationAnimation 不旋转
【发布时间】:2021-08-08 03:20:34
【问题描述】:

我正在尝试将矩形从指定角度旋转到指定角度,但我不确定我是否理解文档。我下面的代码运行,我开始和完成的插槽打印正确的角度。但是矩形没有在屏幕上旋转。我错过了什么?

Rectangle {
  width: 100
  height: 100
   RotationAnimation {
        id: rotateCritter
        duration: 1000
        property real lastAngle: 0
        onStarted: {
            lastAngle = to;
            console.log("Rotating from "+from+" to "+to)
        }
        onStopped: {
            console.log("Done rotating from "+from+" to "+to)
            from = lastAngle;
        }
    }
}


// On click totate the critter to the new angle
rotateCritter.to =  45
rotateCritter.start()

【问题讨论】:

    标签: qt animation rotation qml


    【解决方案1】:

    您的RotationAnimation 缺少target。虽然它是Rectangle 的孩子,但这种关系不会自动使动画的Rectangle 成为target;它必须是明确的。我给了Rectangle 一个idcolor,并把它变成了动画的target

        Rectangle {
            id: critter
            width: 100
            height: 100
            color: "red"
    
            RotationAnimation {
                id: rotateCritter
                target: critter
                duration: 1000
                property real lastAngle: 0
                onStarted: {
                    lastAngle = to;
                    console.log("Rotating from "+from+" to "+to)
                }
                onStopped: {
                    console.log("Done rotating from "+from+" to "+to)
                    from = lastAngle;
                }
            }
        }
    
    

    【讨论】:

      【解决方案2】:

      除了使用RotationAnimation 对象之外,另一个想法是使用Behavior 在矩形自己的rotation 属性上设置动画。

      Rectangle {
          id: rect
          width: 100
          height: 100
          Behavior on rotation {
              NumberAnimation {
                  duration: 1000
              }
          }
      }
      
      rect.rotation = 45   // Animates to 45 degrees
      
      ...
      
      rect.rotation = 0    // Animates back to 0 degrees
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-17
        相关资源
        最近更新 更多