QML类型:ParentAnimation、ParentChange

ParentAnimation

一、描述

ParentAnimation 用于为 Item 的父更改设置动画。

例如,以下 ParentChange 在单击时将 blueRect 更改为 redRect 的子项。 包含 ParentAnimation,它定义了要在 transition 期间应用的 NumberAnimation,可确保项目在移动到其新父项时平滑地进行动画处理:

import QtQuick 2.0

Item 
{
    width: 200; height: 100

    Rectangle 
    {
        id: redRect
        width: 100; height: 100
        color: "red"
    }

    Rectangle 
    {
        id: blueRect
        x: redRect.width
        width: 50; height: 50
        color: "blue"

        states: State
        {
            name: "reparented"
            ParentChange { target: blueRect; parent: redRect; x: 10; y: 10 }
        }

        transitions: Transition 
        {
            ParentAnimation 
            {
                NumberAnimation { properties: "x,y"; duration: 1000 }
            }
        }

        MouseArea { anchors.fill: parent; onClicked: blueRect.state = "reparented" }
    }
}

ParentAnimation 可以包含任意数量的动画。 这些动画默认将并行运行;要按顺序运行它们,请在 SequentialAnimation 中定义它们。

ParentAnimation 通常在 Transition 中与 ParentChange 结合使用。 以这种方式使用时,它会为状态更改期间发生的任何 ParentChange 设置动画。

二、属性成员

1、newParent : Item

要为其设置动画的新父项。

如果 ParentAnimation 是在 Transition 中定义的,则该值默认为在 Transition 的结束状态中定义的值。

2、target : Item

要重新设置父项的项。

在 Transition 中使用时,如果未指定 target ,则所有 ParentChange 事件都由 ParentAnimation 设置动画。


ParentChange

一、描述

ParentChange 可用于重新设置父项,同时保留其在屏幕上的视觉外观(位置、大小、旋转和缩放)。

如果不涉及复杂的变换,ParentChange 只能保留视觉外观。更具体地说,如果为任何涉及重新设置父项的项(即原始和新父项的共同祖先树中的项)设置了转换属性,则它将不起作用。

下面的示例当 blueRect 被点击时,它变为“reparented”状态:它的父对象变为 redRect 并且它位于红色矩形内的 (10, 10) 处,如 ParentChange 中所指定。

import QtQuick 2.9
import QtQuick.Window 2.2

Window
{
    visible: true
    width: 300
    height: 300
    Item
    {
        width: 200; height: 100

        Rectangle
        {
            id: redRect
            width: 100; height: 100
            color: "red"
        }

        Rectangle
        {
            id: blueRect
            x: redRect.width
            width: 50; height: 50
            color: "blue"

            states: State
            {
                name: "reparented"
                ParentChange { target: blueRect; parent: redRect; x: 10; y: 10 }
            }

            transitions: Transition
            {
                ParentAnimation
                {
                    NumberAnimation { properties: "x,y"; duration: 1000 }
                }
            }

            MouseArea { anchors.fill: parent; onClicked: blueRect.state = "reparented" }
        }
    }
}

请注意,与 PropertyChanges 不同,ParentChange 需要一个基于 Item 的目标; 它不适用于任意对象(例如,不能使用它来重新设置 Timer 的父级)。

二、属性成员

1、height : real

      rotation : real

      scale : real

      width : real

      x : real

      y : real

这些属性保存此状态下项目的新位置、大小、比例和旋转。

2、parent : Item

此属性保存处于此状态的项目的新父项。

3、target : Item

此属性保存要重新设置父级的项目。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值