简体   繁体   中英

on_touch_down shouldnt update counter when on_touch_move - kivy

I am running on_touch_down and on_touch_move and in need to update the counter whenever a move is done or touch down is done . However I dont want to update touch down when we just did touch move .but by default even if its just a touch_move it still adds 1 to touch down , i think that is because on move event detects first touch down , but is there any way to fix this :

Below is a sample code .

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout

class JB(BoxLayout):
    def __init__(self, **kwargs):
        super(JB, self).__init__(**kwargs)
        self.touch_move_count = 0
        self.touch_down_count = 0
        self.touch_move_Label = Label()
        self.touch_down_Label = Label()
        self.update_count()
        self.add_widget(self.touch_down_Label)
        self.add_widget(self.touch_move_Label)

    def update_count(self):
        self.touch_down_Label.text = "Touch Down Count : %s"%str(self.touch_down_count)
        self.touch_move_Label.text = "Touch Move Count : %s"%str(self.touch_move_count)

    def on_touch_down(self, touch):
        self.touch_down_count += 1
        print "down", self.touch_down_count
        self.update_count()

    def on_touch_move(self, touch):
        self.touch_move_count += 1
        print "moved", self.touch_move_count
        self.update_count()


class MyJBApp(App):
    def build(self):
        self.parent = JB()
        return self.parent

if __name__ == '__main__':
    MyJBApp().run()

You still touched down on the button before moving - a touch down event will always be sent, because at that moment the application doesn't know if you're going to move or not. If you want to know if a touch happened without a move, you could utilize touch up as well:

def on_touch_down(self, touch):
    self.is_touch_down = True

def on_touch_up(self, touch):
    if self.is_touch_down:
        self.touch_down_count += 1
        print "down", self.touch_down_count
        self.update_count()
    self.is_touch_down = False

def on_touch_move(self, touch):
    self.is_touch_down = False
    self.touch_move_count += 1
    print "moved", self.touch_move_count
    self.update_count()

You could also do something more elaborate by saving the received touch in on_touch_down , then checking in on_touch_up that the position is within some tolerance (like 4px or something).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM