Skip to content

Instantly share code, notes, and snippets.

@quapka
Last active August 29, 2015 14:07
Show Gist options
  • Save quapka/2e72b50815c26cb8f89f to your computer and use it in GitHub Desktop.
Save quapka/2e72b50815c26cb8f89f to your computer and use it in GitHub Desktop.
from kivy.app import App
from kivy.uix.label import Label
from kivy.animation import Animation
class DraggableLabel(Label):
'''A label you can drag upside-down'''
def on_touch_down(self, touch):
if self.collide_point(*touch.pos):
# assure ourselves we will get the updates of this motion
touch.grab(self)
return True
return super(DraggableLabel, self).on_touch_down(touch)
def on_touch_move(self, touch):
if touch.grab_current is self:
# really straightforward...
self.x = touch.x
return True
return super(DraggableLabel, self).on_touch_move(touch)
def on_touch_up(self, touch):
if touch.grab_current is self:
# check if the movement direction was up or down
if touch.dx < 0:
a = Animation(x=0) # down? put the bar all the way down
else:
# A wrong solution
a = Animation(x=self.parent.x) # up? put it at the top
# Right one
a = Animation(x=self.parent.width - self.width) # up? put it at the top
a.start(self) # actually start the animation
return True
return super(DraggableLabel, self).on_touch_up(touch)
class DragLabelApp(App):
pass
if __name__ == '__main__':
DragLabelApp().run()
FloatLayout:
FloatLayout:
Label:
center: root.center
text: 'test content'
size_hint: None, None
size: self.texture_size
ScrollView:
x: dg.x
y: root.y
size_hint_x: None
do_scroll_x: True
do_scroll_y: False
width: root.x - dg.x
canvas:
Color:
rgba: 0, 0, 0, .8
Rectangle:
pos: self.pos
size: self.size
# the actual notification container, with placeholder content
BoxLayout:
size_hint_x: None
width: 500
orientation: 'vertical'
Label:
text: 'test'
Label:
text: 'test'
Label:
text: 'test'
Label:
text: 'test'
Label:
text: 'test'
Label:
text: 'test'
Label:
text: 'test'
Label:
text: 'test'
Label:
text: 'test'
DraggableLabel:
canvas.before:
Color:
rgba: 0,0,1,1
Rectangle:
pos: self.pos
size: self.size
size_hint_x: None
x: root.x
id: dg
width: '50pt'
text: 'Drag me'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment