【问题标题】:Qt Quick: Code redundancy when creating TransitionQt Quick:创建转换时的代码冗余
【发布时间】:2016-11-08 14:56:14
【问题描述】:

我的代码:

states: [
    State {
        name: "pressed"; when: mouseArea.pressed
        PropertyChanges {
            target: foo
            prop1: 10
            prop2: 10
            prop3: 10
        }
    },
    State {
        name: "notPressed"; when: !mouseArea.pressed
        PropertyChanges {
            target: foo
            prop1: 1
            prop2: 1
            prop3: 1
        }
    }
]
transitions: [
    Transition {
        to: "*"
        NumberAnimation {
            target: foo
            properties: "prop1,prop2,prop3"
            duration: 1000
        }
    }
]

这可行,但当PropertyChanges 元素中已指定要更改的属性时,我需要冗余指定properties: "prop1,prop2,prop3"。另外,当 PropertyChanges 元素中已经指定 target: foo 时,我需要在 NumberAnimation 中冗余指定它。

可以避免这种冗余吗?如果没有,为什么不呢?

【问题讨论】:

    标签: qt animation qml qtquick2


    【解决方案1】:

    属性更改并不一定意味着它会被动画化。并且所有属性更改也不必具有相同的动画。

    我在这里看不到冗余,如果您想要的行为是默认行为,您将无法对所发生的事情进行细粒度控制。对于所有属性更改,您都会遇到相同的行为,这可能适合您的特定需求,但在所有其他情况下实际上会出现很大问题。

    【讨论】:

      【解决方案2】:

      正如@ddriver 所说,这需要对状态更改期间的动画内容进行精细控制。

      但是,如果您的所有属性都更改为与示例中相同的值,您可以对其进行重构并将它们绑定到一个公共属性。 像这样:

      property int bar: mouseArea.pressed ? 10 : 1
      prop1: bar
      prop2: bar
      prop3: bar
      
      states: [
          State {
              name: "pressed"; when: mouseArea.pressed
              PropertyChanges {
                  target: foo
                  bar: 10
              }
          },
          State {
              name: "notPressed"; when: !mouseArea.pressed
              PropertyChanges {
                  target: foo
                  bar: 1
              }
          }
      ]
      transitions: [
          Transition {
              to: "*"
              NumberAnimation {
                  target: foo
                  property: "bar"
                  duration: 1000
              }
          }
      ]
      

      或者,如果您的转换是对称的(使用从状态 A -> 状态 B 和从 B -> A 的相同动画),您可以使用 Behavior 来简化您的代码:

      property int bar: mouseArea.pressed ? 10 : 1
      prop1: bar
      prop2: bar
      prop3: bar
      
      Behavior on bar {
          NumberAnimation { duration: 1000 }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多