使用 ScaleRotateGestureHandler 来处理捏合和旋转手势。 识别仅限于元素的几何范围内。
ScaleRoteGestureHandler 在所有平台上支持触摸屏,此外还支持 macOS 和 iOS 上的触控板手势。
export component Example inherits Window { width: 400px; height: 400px;
property <float> start-scale; property <angle> start-rotation;
gesture := ScaleRotateGestureHandler { started => { start-scale = rect.current-scale; start-rotation = rect.current-rotation; }
updated => { rect.current-scale = start-scale * self.scale; rect.current-rotation = start-rotation + self.rotation; }
rect := Rectangle { background: @radial-gradient(circle, #4488ff, #224488); border-radius: 8px;
property <float> current-scale: 1.0; property <angle> current-rotation: 0deg; width: 200px * self.current-scale; height: 200px * self.current-scale; x: (parent.width - self.width) / 2; y: (parent.height - self.height) / 2;
Text { text: "Pinch & rotate"; color: white; } } }}scale 属性提供相对于手势开始时的累积缩放因子(起始为 1.0)。 rotation 属性提供累积的旋转角度(起始为 0deg)。 使用 started 回调来捕获初始状态,然后在 updated 回调中乘以 scale 并加上 rotation 来应用手势。
属性
enabled
bool default: true
禁用后,ScaleRotateGestureHandler 不会识别任何手势,并且任何正在进行的手势都会被取消。
active
bool (out) default: false
手势正在识别时为 true,否则为 false。
scale
float (out) default: 1.0
手势的累积缩放因子。手势开始时始终为 1.0。 大于 1.0 表示放大,小于 1.0 表示缩小。 手势未激活时,该值为 1.0。
rotation
angle (out) default: 0deg
手势的累积旋转角度。手势开始时始终为 0deg。 正值表示顺时针旋转,负值表示逆时针旋转。 手势未激活时,该值为 0deg。
center
struct Point (out) default: a struct with all default values
手势的中心点,位于 ScaleRotateGestureHandler 的坐标系中。 对于双指触摸输入,这是两指之间的中点。 对于触控板手势,这是鼠标光标位置。
Point
此结构表示具有 x 和 y 坐标的点
x(length):y(length):
回调
started()
在手势开始时调用。使用它捕获要变换的初始状态。
updated()
在手势进行中,当 scale、rotation 或 center 发生变化时调用。
ended()
在手势正常完成(手指抬起)时调用。
cancelled()
在手势被取消时调用,例如在活动手势期间该处理器被禁用或窗口失去焦点。