如何在python kivy应用程序中左右滑动?

2024-05-14 17:24:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在我的kivy应用程序中添加一个刷卡事件,据我所知kivy没有on_touch_left或{}这样的事件,但它有另一个on_touch_move函数,我认为可以用于此目的

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

我在上面的代码中注意到,如果我们向右滑动touch.x值会增加,如果我们向右滑动{}值就会减小。我们只需要利用第一个和最后一个touch.x值之间的差异来预测左/右滑动。在

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


Tags: 函数self目的应用程序moveondef事件
3条回答
def on_touch_move(self,touch):
    if touch.dx > 0:
        self.ids.scrn_mnger.transition.direction = 'right'
        self.ids.scrn_mnger.current = 'scrn_open'
    elif touch.dx < 0:
        self.ids.scrn_mnger.transition.direction = 'left'
        self.ids.scrn_mnger.current = 'scrn_media'

不使用on_touch_move事件,可以使用on_touch_down并保存touch.x,然后使用on_touch_up并比较{},例如:

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来设置一个最小的刷卡范围来执行某些操作。在

我使用on-touch-down触摸.dx触摸.dy属性来计算这个值。原因是我需要动态计算刷卡的长度,因为它决定了图像的alpha值。对于非动态计算,我发现moea的解决方案更直接,资源消耗更少。在

    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"

相关问题 更多 >

    热门问题