Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在QML ScrollView中设置滚动动画?_Qml_Qtquick2 - Fatal编程技术网

如何在QML ScrollView中设置滚动动画?

如何在QML ScrollView中设置滚动动画?,qml,qtquick2,Qml,Qtquick2,如何在QML滚动视图中设置滚动动画 我在contentItem.contentY上尝试了行为,但不起作用。使用Qt Quick Controls 1 您只需对属性flickableetem.contentY上的值更改设置动画即可 一个简单的例子: Item { anchors.fill: parent ColumnLayout { anchors.fill: parent Button { id: btn

如何在QML滚动视图中设置滚动动画

我在
contentItem.contentY上尝试了
行为
,但不起作用。

使用Qt Quick Controls 1 您只需对属性
flickableetem.contentY
上的值更改设置动画即可

一个简单的例子:

Item {
    anchors.fill: parent
    ColumnLayout {
        anchors.fill: parent
        Button {
            id: btn
            onClicked: scroll.scrollTo(scroll.flickableItem.contentY + 100)
        }

        ScrollView {
            id: scroll
            function scrollTo(y) {
                scrollAnimation.to = y
                scrollAnimation.start()
            }

            NumberAnimation on flickableItem.contentY {
                id: scrollAnimation
                duration: 1000
            }
            contentItem: Column {
                Repeater {
                        model: 30
                        Rectangle {
                            width: 100; height: 40
                            border.width: 1
                            color: "yellow"
                        }
                    }
            }
        }
    }
}
当你点击按钮时,它将以100像素的速度平滑地滚动

使用Qt快速控制2
flickableItem.contentY
不再可用。在Qt Quick Controls 2中执行相同操作的最简单方法是设置
滚动条的位置动画

请注意,
QScrollBar
的位置以百分比为单位(表示在0和1之间),而不是以像素为单位

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")
    ScrollView {
        id: scroll
        width: 200
        height: 200
        clip: true
        function scrollTo(y) {
            scrollAnimation.to = y
            scrollAnimation.start()
        }
        ScrollBar.vertical: ScrollBar {
            id: test
            parent: scroll
            x: scroll.mirrored ? 0 : scroll.width - width
            y: scroll.topPadding
            height: scroll.availableHeight
            active: scroll.ScrollBar.horizontal.active
            policy: ScrollBar.AlwaysOn

            NumberAnimation on position {
                id: scrollAnimation
                duration: 1000
            }
        }
        ListView {
            model: 20
            delegate: ItemDelegate {
                text: "Item " + index
            }
        }
    }
    Button {
        id: btn
        anchors.top: scroll.bottom
        onClicked: scroll.scrollTo(test.position + 0.1)
    }
}

当你点击按钮时,它将滚动10%的高度。

我想你应该先解释一下你说的
动画
动画滚动是什么意思?让它看起来像一个平滑的滚动到位置,而不是一个即时跳转。请提供,因为它不太清楚你在问什么。我的意思是:我不能在QML中演示,因为我不知道如何在QML中做:)谢谢!但是通过测试,不可能在
flickable
上设置
NumberAnimation
——这在Qt5.12中不是可识别的属性。这是我一直面临的主要问题。。。正确的语法是什么?我的第一个例子是Qt Quick Controls 1。我已经用Qt Quick Controls 2的示例更新了我的答案。应该有一个不创建新滚动条的解决方案。但是,代码将更难理解。