4

我正在尝试基于 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
        }
    }
}

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

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,但它会导致跳过项目而不是平滑过渡。

4

1 回答 1

3

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

对于SwipeView页面,除非您想使用类似的包装器,否则您可以使用TranslateQML 类型应用转换。您可以访问SwipeView's internalListView及其contentXvia SwipeView.contentItem。计算所需的视差效果留给读者作为练习。:)

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

于 2016-10-29T19:53:44.630 回答