PinchArea
可以参看PinchArea文档。下面是SDK自带的demo。通过此demo可以了解PinchArea的使用。 PinchArea实现了“挤压”操作。 **要点:**在PinchArea中可以使用一个内置的pinch参数
。这个参数是一个PinchEvent实例(instance)。 而pinch内置参数中,有一个target属性用来标识被控对象的。
-
使用 pinch.target 这种方式,你什么都不用关心,甚至不需要弄明白 pinch 属性到底是什么含义,就可以得到一个不错的变换效果, Qt Quick 默认帮你处理所有的事情。
-
使用 onPinchStarted() / onPinchUpdated() / onPinchFinished() 要稍微麻烦一些,你必须要了解 PinchEvent 每个参数的含义,自己设计变换策略。不过好处是,七十二般变化都由你控制。
<!-- lang: js -->
import QtQuick 1.1 Rectangle { width: 640 height: 360 color: "gray" Flickable { id: flick anchors.fill: parent contentWidth: 500 contentHeight: 500 PinchArea { width: Math.max(flick.contentWidth, flick.width) height: Math.max(flick.contentHeight, flick.height) property real initialWidth property real initialHeight onPinchStarted: { initialWidth = flick.contentWidth initialHeight = flick.contentHeight } onPinchUpdated: { // adjust content pos due to drag flick.contentX += pinch.previousCenter.x - pinch.center.x flick.contentY += pinch.previousCenter.y - pinch.center.y // resize content flick.resizeContent(initialWidth * pinch.scale, initialHeight * pinch.scale, pinch.center) } onPinchFinished: { // Move its content within bounds. flick.returnToBounds() } Rectangle { width: flick.contentWidth height: flick.contentHeight color: "white" Image { anchors.fill: parent source: "qt-logo.jpg" MouseArea { anchors.fill: parent onDoubleClicked: { flick.contentWidth = 500 flick.contentHeight = 500 } } } } } } }
其他不再赘述。很简单。
PinchArea的使用
参考博文:Qt Quick 事件处理之捏拉缩放与旋转 缩放与旋转实现要点: 要想使用 PinchArea 来变换一个 Item ,有两个办法:
- 设定 target 属性,将其指向要变换的 Item ,然后 PinchArea 就会在合适的时候帮你变换它。
- 处理 onPinchStarted() / onPinchUpdated() / onPinchFinished() 信号,在信号处理器中变换目标 Item 。这种方式更灵活,你甚至可以同时处理多个 Item 。
选定一种方式后,你可能还要配置 PinchArea.pinch 属性,给不同的参数设置合理的值,比方说最大可以放大到多少倍。scale、angle等属性此时就发挥巨大作用了!