QML:SwipeView中项目的中间滑动位置

时间:2016-10-27 06:12:06

标签: qt listview qml swipeview qtquickcontrols2

我正在尝试基于SwipeView元素创建视差视图。 QML文档中的示例说明了如何使用ListView实现它:

Image {
    id: background
    source: "background.png"
    fillMode: Image.TileHorizontally
    x: -list.contentX / 2
    width: Math.max(list.contentWidth, parent.width)
}

ListView {
    id: list
    anchors.fill: parent
    spacing: 20
    snapMode: ListView.SnapToItem
    orientation: ListView.Horizontal
    highlightRangeMode: ListView.StrictlyEnforceRange
    boundsBehavior: Flickable.StopAtBounds
    maximumFlickVelocity: 1000

    model: _some_cpp_list
    //Loader is used as a workaround for QTBUG-49224
    delegate: Loader {
        id: loaderDelegate
        source: "MyDelegate.qml"
        width: myScreen.width
        height: myScreen.height
        onLoaded: {
            loaderDelegate.item.logic = modelData
        }
    }
}

现在,这可行,但ListView的实例我想使用SwipeView,因为它需要更少的代码来实现我想要的行为:

SwipeView {
    id: list
    anchors.fill: parent
    spacing: 20
    Repeater {
        model: _some_cpp_list
        delegate: MyDelegate {
            logic: modelData
        }
    }

有什么方法可以访问SwipeView当前的“x”位置或滑动偏移量,以便在此行中使用:

x: -list.contentX / 2

到目前为止我发现的最接近的是x: -swipeView.contentData[0].x / 2,但它会导致跳过项而不是平滑过渡。

1 个答案:

答案 0 :(得分:3)

您无法控制水平ListView中项目的x坐标,因为项目位置由ListView管理。您能够在第一个示例中操作项目位置的原因是您实际上并未操纵委托位置,而是另一个项目包含在Loader委托中。

对于SwipeView页面,除非您想使用类似的包装器,否则可以使用Translate QML类型应用转换。您可以通过SwipeView访问ListView的内部contentX及其SwipeView.contentItem。计算所需的视差效果留给读者练习。 :)

PS。另请参阅https://doc.qt.io/qt-5/qtquick-views-example.html上的ParallaxView示例。