我正在尝试基于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 回答
您无法控制水平
ListView
中项目的x坐标,因为项目位置由ListView
管理 . 您're able to manipulate the item positions in your first example is that you'之所以没有实际操作委托位置,而是另一个项目包含在Loader
委托中 .对于
SwipeView
页面,除非您要使用类似的包装器,否则可以使用Translate
QML类型应用转换 . 您可以通过SwipeView.contentItem
访问SwipeView
的内部ListView
及其contentX
. 计算所需的视差效果留给读者练习 . :)PS . 另见https://doc.qt.io/qt-5/qtquick-views-example.html的
ParallaxView
示例 .