旋转整个小部件,而不仅仅是它的 canvas
rotate entire widget, not only its canvas
在这个example中,我们可以看到canvas的旋转。但是单击按钮的角未注册。如何旋转整个按钮?
您或许可以覆盖它的 collide_point
方法,通过变换触摸坐标来解释旋转。
如果您使用小部件系统(例如,将 Button 放在 Scatter 中),碰撞会为您处理。
根据 inclement 的回答,我在分散布局上创建了一个按钮,它的 hitbox 设置正确。
from kivy.app import App
from kivy.lang import Builder
kv = '''
FloatLayout:
ScatterLayout:
size_hint: None, None
size: 200, 200
pos_hint: {'center_x': .5, 'center_y': .5}
rotation: 45
do_rotation: False
do_scale: False
do_translation: False
Button:
text: 'hello world'
'''
class RotationApp(App):
def build(self):
return Builder.load_string(kv)
RotationApp().run()
我使用分散布局而不是分散布局,因为它将其大小传递给子部件。本例中不需要 do_x: False
,因为按钮会拦截触摸事件,但如果我放置标签,它会在触摸时移动。
在这个example中,我们可以看到canvas的旋转。但是单击按钮的角未注册。如何旋转整个按钮?
您或许可以覆盖它的 collide_point
方法,通过变换触摸坐标来解释旋转。
如果您使用小部件系统(例如,将 Button 放在 Scatter 中),碰撞会为您处理。
根据 inclement 的回答,我在分散布局上创建了一个按钮,它的 hitbox 设置正确。
from kivy.app import App
from kivy.lang import Builder
kv = '''
FloatLayout:
ScatterLayout:
size_hint: None, None
size: 200, 200
pos_hint: {'center_x': .5, 'center_y': .5}
rotation: 45
do_rotation: False
do_scale: False
do_translation: False
Button:
text: 'hello world'
'''
class RotationApp(App):
def build(self):
return Builder.load_string(kv)
RotationApp().run()
我使用分散布局而不是分散布局,因为它将其大小传递给子部件。本例中不需要 do_x: False
,因为按钮会拦截触摸事件,但如果我放置标签,它会在触摸时移动。