Qt Quick:创建Transition时的代码冗余

时间:2016-11-08 14:56:14

标签: qt animation qml qtquick2

我的代码:

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
        }
    }
]

这有效,但要求我在properties: "prop1,prop2,prop3"元素中指定要更改的属性时冗余指定PropertyChanges。此外,我需要在NumberAnimation中冗余地指定target: foo,如果它已在PropertyChanges元素中指定。

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

2 个答案:

答案 0 :(得分:4)

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

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

答案 1 :(得分:2)

正如@ddriver所说,需要对状态变化期间的动画进行精确控制。

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

[UPDATE CM_CAMPAIGN_EMAIL_LOG SET MAIL_BOUNCE = 1 , 
DESCRIPTION = "over quota" , EVENT="bounce" 
WHERE CONTACT_ID IN( 1372606012,1773400728) AND TEMPLATE_ID = 1638839861 AND CAMPAIGN_ID = 198673431, 
UPDATE CM_CONTACT_DETAILS SET SEND_GRID_BOUNCE = 'Y' ,
SEND_GRID_BOUNCE_REASON = "User's mailbox was unavailable or could not be found or because of incoming policy reasons"  
WHERE EMAIL_IDS = 'marmstrong336@verizon.net', 
UPDATE CM_LEAD_DETAILS SET SEND_GRID_BOUNCE = 'Y' , 
SEND_GRID_BOUNCE_REASON = "User's mailbox was unavailable or could not be found or because of incoming policy reasons"  
WHERE EMAIL_IDS = 'marmstrong336@verizon.net', 
UPDATE CM_CAMPAIGN_EMAIL_LOG SET EVENT="delivered" 
WHERE CONTACT_ID IN( 448520112)  AND TEMPLATE_ID = 1638839861 AND CAMPAIGN_ID = 198673431, 
UPDATE CM_CAMPAIGN_EMAIL_LOG SET EVENT="delivered" 
WHERE CONTACT_ID IN( 1773405252)  AND TEMPLATE_ID = 1638839861 AND CAMPAIGN_ID = 198673431]

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

CM_CAMPAIGN_EMAIL_LOG