使用 SwipeGestureHandler 来处理特定方向的滑动手势。 其识别范围仅限于元素的几何范围内。
SwipeGestureHandler 可识别触摸屏的滑动和鼠标的拖动。
slint
export component Example inherits Window { width: 270px; height: 100px;
property <int> current-page: 0;
sgr := SwipeGestureHandler { handle-swipe-right: current-page > 0; handle-swipe-left: current-page < 5; swiped => { if self.current-position.x > self.pressed-position.x + self.width / 4 { current-page -= 1; } else if self.current-position.x < self.pressed-position.x - self.width / 4 { current-page += 1; } }
HorizontalLayout { property <length> position: - current-page * root.width; animate position { duration: 200ms; easing: ease-in-out; } property <length> swipe-offset; x: position + swipe-offset; states [ swiping when sgr.swiping : { swipe-offset: sgr.current-position.x - sgr.pressed-position.x; out { animate swipe-offset { duration: 200ms; easing: ease-in-out; } } } ]
Rectangle { width: root.width; background: green; } Rectangle { width: root.width; background: limegreen; } Rectangle { width: root.width; background: yellow; } Rectangle { width: root.width; background: orange; } Rectangle { width: root.width; background: red; } Rectangle { width: root.width; background: violet; } } }}通过设置 handle-swipe-left/right/up/down 属性来指定你希望处理的不同滑动方向,并在 swiped 回调中响应手势。
识别器区域上的指针按下事件会以较短延迟转发给子元素。 如果指针在某个已启用的滑动方向上移动超过 8 个逻辑像素,则手势被识别,并且事件不再转发给子元素。
属性
enabled
bool default: true
禁用后,SwipeGestureHandler 不会识别任何手势。
pressed-position
struct Point (out) default: a struct with all default values
滑动开始时指针的位置。
Point
此结构表示具有 x 和 y 坐标的点
x(length):y(length):
current-position
struct Point (out) default: a struct with all default values
当前指针位置。
Point
此结构表示具有 x 和 y 坐标的点
x(length):y(length):
swiping
bool (out) default: false
手势被识别期间为 true,否则为 false。
处理滑动方向的属性
handle-swipe-left
bool default: false
handle-swipe-right
bool default: false
handle-swipe-up
bool default: false
handle-swipe-down
bool default: false
回调
moved()
当指针移动时被调用。
swiped()
在滑动手势被识别且指针释放之后被调用。
cancelled()
当滑动被以编程方式取消或窗口失去焦点时被调用。
函数
cancel()
取消任何进行中的滑动手势识别。