我想在我的 kivy 应用程序中添加一个滑动事件,据我所知,kivy 没有像 on_touch_left
这样的事件。或 on_touch_right
可用,但它还有另一个 on_touch_move
我认为可以用于此目的的功能
class TestWidget(BoxLayout):
def on_touch_move(self, touch):
print touch.x
我在上面的代码中注意到,如果我们向右滑动 touch.x
值增加,如果我们向右滑动 touch.x
值(value)下降。我们只需要取第一个和最后一个之间的差异touch.x
预测左/右滑动的值。
问题是如何存储和检索touch.x
从初始值到最终值。
最佳答案
除了使用 on_touch_move
事件,您可以使用 on_touch_down
并保存 touch.x
然后使用 on_touch_up
和比较touch.x
,例如:
initial = 0
def on_touch_down(self, touch):
initial = touch.x
def on_touch_up(self, touch):
if touch.x > initial:
# do something
elif touch.x < initial:
# do other thing
else:
# what happens if there is no move
更好的方法是比较使用 if touch.x - initial > some-value
设置最小滑动范围来执行某些操作。
关于python - 如何在 python kivy 应用程序中左右滑动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45144228/