How to know if user is scrolling up or scrolling down in Kivy?

370 views Asked by At

I am trying to make an Application in kivy which uses ScrollView. Is there any way through which I can know if user is scrolling down or scrolling up.

1

There are 1 answers

1
Nykakin On

You can store mouse position received in on_scroll_move and then determine the direction by comparing value you have now with value you've saved before.

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder


Builder.load_string('''
<MyWidget>:
    ScrollView:
        on_scroll_start: root.scroll_pos_y = args[1].pos[1]
        on_scroll_move: root.scroll_direction(args[1].pos[1])

        Label:
            text: 'test'
            size_hint_y: None
            height: 1000
''')

class MyWidget(BoxLayout):
    scroll_pos_y = 0

    def scroll_direction(self, new_scroll_pos_y):
        if new_scroll_pos_y - self.scroll_pos_y < 0:
            print('up')
        else:
            print('down')

        self.scroll_pos_y = new_scroll_pos_y

class MyApp(App):
    def build(self):
        return MyWidget()

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