사용 사례 - QML의 애니메이션

Qt Quick 에서는 프로퍼티에 애니메이션을 적용하는 기능을 제공합니다. 속성 애니메이션을 사용하면 속성 값이 대상 값으로 즉시 변경되지 않고 중간 값을 통해 이동할 수 있습니다. 예를 들어 항목의 위치에 애니메이션을 적용하려면 항목의 위치(x 및 y)를 제어하는 속성에 애니메이션을 적용하여 항목의 위치가 목표 위치로 이동하는 동안 매 프레임마다 변경되도록 할 수 있습니다.

플루이드 UI

QML은 플루이드 UI를 쉽게 제작할 수 있도록 설계되었습니다. 이는 UI 컴포넌트가 갑자기 나타나거나 사라지거나 점프하는 대신 애니메이션을 적용하는 사용자 인터페이스입니다. Qt Quick 에서는 UI 컴포넌트가 새로운 위치에 즉시 나타나는 대신 애니메이션과 함께 움직이도록 하는 두 가지 간단한 방법을 제공합니다.

상태 및 전환

Qt Quick 를 사용하면 State 객체에서 다양한 UI 상태를 선언할 수 있습니다. 이러한 상태는 기본 상태의 속성 변경으로 구성되며 UI 로직을 구성하는 데 유용한 방법이 될 수 있습니다. 트랜지션은 항목과 연결하여 상태 변경으로 인해 속성이 변경될 때 해당 속성이 애니메이션되는 방식을 정의할 수 있는 객체입니다.

항목의 상태 및 전환은 Item::statesItem::transitions 속성을 사용하여 선언할 수 있습니다. 상태는 일반적으로 컴포넌트의 루트 항목인 항목의 상태 목록 속성 내에서 선언됩니다. 동일한 항목에 정의된 트랜지션은 상태 변경에 애니메이션을 적용하는 데 사용됩니다. 다음은 예시입니다.

Item {
    id: container
    width: 320
    height: 120

    Rectangle {
        id: rect
        color: "red"
        width: 120
        height: 120

        TapHandler {
            onTapped: container.state === '' ? container.state = 'other' : container.state = ''
        }
    }
    states: [
        // This adds a second state to the container where the rectangle is farther to the right

        State { name: "other"

            PropertyChanges {
                target: rect
                x: 200
            }
        }
    ]
    transitions: [
        // This adds a transition that defaults to applying to all state changes

        Transition {

            // This applies a default NumberAnimation to any changes a state change makes to x or y properties
            NumberAnimation { properties: "x,y" }
        }
    ]
}

프로퍼티 변경 애니메이션.

동작을 사용하여 프로퍼티가 변경될 때 사용할 애니메이션을 지정할 수 있습니다. 그러면 소스에 관계없이 모든 변경 사항에 적용됩니다. 다음 예는 동작을 사용하여 화면에서 움직이는 버튼에 애니메이션을 적용하는 예제입니다.

Item {
    width: 320
    height: 120

    Rectangle {
        color: "green"
        width: 120
        height: 120

        // This is the behavior, and it applies a NumberAnimation to any attempt to set the x property
        Behavior on x {

            NumberAnimation {
                //This specifies how long the animation takes
                duration: 600
                //This selects an easing curve to interpolate with, the default is Easing.Linear
                easing.type: Easing.OutBounce
            }
        }

        TapHandler {
            onTapped: parent.x == 0 ? parent.x = 200 : parent.x = 0
        }
    }
}

기타 애니메이션

모든 애니메이션을 특정 프로퍼티나 상태에 연결할 필요는 없습니다. 보다 일반적으로 애니메이션을 만들고 애니메이션 내부에 대상 항목과 속성을 지정할 수도 있습니다. 다음은 이를 수행하는 다양한 방법의 몇 가지 예입니다:

Item {
    width: 320
    height: 120

    Rectangle {
        color: "blue"
        width: 120
        height: 120

        // By setting this SequentialAnimation on x, it and animations within it will automatically animate
        // the x property of this element
        SequentialAnimation on x {
            id: xAnim
            // Animations on properties start running by default
            running: false
            loops: Animation.Infinite // The animation is set to loop indefinitely
            NumberAnimation { from: 0; to: 200; duration: 500; easing.type: Easing.InOutQuad }
            NumberAnimation { from: 200; to: 0; duration: 500; easing.type: Easing.InOutQuad }
            PauseAnimation { duration: 250 } // This puts a bit of time between the loop
        }

        TapHandler {
            // The animation starts running when you click within the rectangle
            onTapped: xAnim.running = true
        }
    }
}
Item {
    width: 320
    height: 120

    Rectangle {
        id: rectangle
        color: "yellow"
        width: 120
        height: 120

        TapHandler {
            // The animation starts running when you click within the rectangle
            onTapped: anim.running = true;
        }
    }

    // This animation specifically targets the Rectangle's properties to animate
    SequentialAnimation {
        id: anim
        // Animations on their own are not running by default
        // The default number of loops is one, restart the animation to see it again

        NumberAnimation { target: rectangle; property: "x"; from: 0; to: 200; duration: 500 }

        NumberAnimation { target: rectangle; property: "x"; from: 200; to: 0; duration: 500 }
    }
}

애니메이션에 대한 자세한 내용은 Qt Quick - 상태, 전환 및 애니메이션 페이지중요 개념에서 확인할 수 있습니다.

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.