简体   繁体   English

如何在 Kivy 的 ScrollView 边缘移动 cursor 时滚动 ScrollView

[英]How to scroll ScrollView when move cursor at the edge of ScrollView in Kivy

So basically I'm using draggable function from Kivy Garden, and currently using it with ScrollView, example can be found here.所以基本上我正在使用来自 Kivy Garden 的可拖动function,并且目前将它与 ScrollView 一起使用, 示例可以在这里找到。

The problem is that when I'm dragging a widget, I only can move it inside my viewport.问题是当我拖动一个小部件时,我只能在我的视口内移动它。 Because the ScrollView does not auto scroll when you drag and move it to the edge of the Layout.因为当您将 ScrollView 拖动并移动到 Layout 的边缘时,它不会自动滚动。

So my question is there any way to make the ScrollView auto scroll when my cursor is at the edge of the ScrollView layout without using the mouse's wheel?所以我的问题是,当我的 cursor 位于 ScrollView 布局的边缘而不使用鼠标滚轮时,有什么方法可以让 ScrollView 自动滚动?

You can extend your DraggableItem to force scrolling when the DraggableItem is at the top or bottom of the ScrollView .DraggableItem位于ScrollView的顶部或底部时,您可以扩展DraggableItem以强制滚动。 Here is an idea about how to do this, but it may need more work:这是一个关于如何做到这一点的想法,但它可能需要更多的工作:

class DraggableItem(KXDraggableBehavior, Button):
    scroll_up_event = ObjectProperty(None, allownone=True)
    scroll_down_event = ObjectProperty(None, allownone=True)

    def on_touch_move(self, touch):
        super(DraggableItem, self).on_touch_move(touch)
        if self.is_being_dragged:
            self.handle_scrolling()

    def handle_scrolling(self):
        global root
        if self.y < root.y:
            # scroll down
            if self.scroll_down_event is None:
                self.scroll_down_event = Clock.schedule_interval(self.do_scroll_down, 0.2)
        elif self.y >= root.y:
            # stop any down scrolling
            if self.scroll_down_event:
                self.scroll_down_event.cancel()
                self.scroll_down_event = None
        if self.top > root.top:
            # scroll up
            if self.scroll_up_event is None:
                self.scroll_up_event = Clock.schedule_interval(self.do_scroll_up, 0.2)
        elif self.top <= root.top:
            # stop any up scrolling
            if self.scroll_up_event:
                self.scroll_up_event.cancel()
                self.scroll_up_event = None

    def do_scroll_up(self, *args):
        global root
        scroll = root.scroll_y + 0.01
        if scroll <= 1:
            root.scroll_y = scroll
        else:
            # cannot scroll up any farther
            self.scroll_up_event.cancel()
            self.scroll_up_event = None

    def do_scroll_down(self, *args):
        global root
        scroll = root.scroll_y - 0.01
        if scroll >= 0:
            root.scroll_y = scroll
        else:
            # cannot scroll down any farther
            self.scroll_down_event.cancel()
            self.scroll_down_event = None

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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