2017-07-17 65 views
1

我想在我的kivy应用程序,并在我的知识kivy添加刷卡事件没有如on_touch_lefton_touch_right可用的事件,但它有另一个on_touch_move功能,我认为可以用于此目的如何在python kivy应用程序中左右滑动?

class TestWidget(BoxLayout): 
    def on_touch_move(self, touch): 
     print touch.x 

我在上面的代码中注意到的是,如果我们向右滑动touch.x值增加,并且如果我们向右滑动touch.x值减少。我们只需将第一个和最后一个touch.x值之间的差异用于预测左/右滑动。

问题是如何存储和检索从初始值到最终值的touch.x值。

+1

我觉得这个[问题](https://stackoverflow.com/questions/30934445/kivy-swiping-carousel-screenmanager)是类似的,可以帮助你。 – KelvinS

+0

接受的答案必须导入手势模块,我不喜欢这样。 – Eka

回答

1

而不是使用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设定最低刷卡范围做比较一些行动。

+0

这是一个很棒的答案,我没有想到,谢谢。因为我在一个类中使用了这个函数,所以'initial'有一个小问题,它必须是函数内部的'self.initial'。我也采取了你的最后建议,我作为百分比,而不是差异,它的作品令人惊叹 – Eka

+0

是的,我错过了'self.initial'部分,但我很高兴它的工作:) –

1

我用on_touch_downtouch.dxtouch.dy属性一起计算这个。原因是我需要动态计算滑动的长度,因为它决定了图像的alpha值。对于非动态计算,我发现Moe A的解决方案更直接,资源更少。

def on_touch_move(self, touch): 
     if self.enabled: 
      self.x_total += touch.dx 
      self.y_total += touch.dy 

      if abs(self.x_total) > abs(self.y_total): 
       "do something" 
      else: 
       "do something else"