栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在奇异果中的触摸事件上旋转对象

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在奇异果中的触摸事件上旋转对象

您可以将canvas的角度绑定到

NumericProperty
,以从代码内部对其进行更改。您需要做的就是正确计算这些角度。在玩了一点之后,我创建了以下代码:

from kivy.app import Appfrom kivy.uix.widget import Widgetfrom kivy.lang import Builderfrom kivy.animation import Animationfrom kivy.properties import NumericPropertyimport mathkv = '''<Dial>:    canvas:        Rotate: angle: root.angle origin: self.center        Color: rgb: 1, 0, 0        Ellipse:     size: min(self.size), min(self.size) pos: 0.5*self.size[0] - 0.5*min(self.size), 0.5*self.size[1] - 0.5*min(self.size)        Color: rgb: 0, 0, 0        Ellipse:     size: 50, 50 pos: 0.5*root.size[0]-25, 0.9*root.size[1]-25'''Builder.load_string(kv)class Dial(Widget):    angle = NumericProperty(0)    def on_touch_down(self, touch):        y = (touch.y - self.center[1])        x = (touch.x - self.center[0])        calc = math.degrees(math.atan2(y, x))        self.prev_angle = calc if calc > 0 else 360+calc        self.tmp = self.angle    def on_touch_move(self, touch):        y = (touch.y - self.center[1])        x = (touch.x - self.center[0])        calc = math.degrees(math.atan2(y, x))        new_angle = calc if calc > 0 else 360+calc        self.angle = self.tmp + (new_angle-self.prev_angle)%360    def on_touch_up(self, touch):        Animation(angle=0).start(self)class DialApp(App):    def build(self):        return Dial()if __name__ == "__main__":    DialApp().run()

我正在计算初始(按鼠标后)和以后的角度之间的差异

on_touch_move
。由于angle是一个属性,因此我也可以使用它来修改它,
kivy.animation
以在释放鼠标按钮后使拨盘回旋。

编辑

on_touch_down
子圈子活动:

from kivy.app import Appfrom kivy.uix.widget import Widgetfrom kivy.uix.floatlayout import FloatLayoutfrom kivy.lang import Builderfrom kivy.animation import Animationfrom kivy.properties import NumericPropertyimport mathkv = '''<Dial>:    circle_id: circle_id    size: root.size    pos: 0, 0    canvas:        Rotate: angle: self.angle origin: self.center        Color: rgb: 1, 0, 0        Ellipse:     size: min(self.size), min(self.size) pos: 0.5*self.size[0] - 0.5*min(self.size), 0.5*self.size[1] - 0.5*min(self.size)    Circle:        id: circle_idsize_hint: 0, 0        size: 50, 50        pos: 0.5*root.size[0]-25, 0.9*root.size[1]-25        canvas: Color:     rgb: 0, 1, 0 Ellipse:         size: 50, 50     pos: self.pos   '''Builder.load_string(kv)class Circle(Widget):    def on_touch_down(self, touch):        if self.collide_point(*touch.pos): print "small circle clicked"class Dial(Widget):    angle = NumericProperty(0)    def on_touch_down(self, touch):        if not self.circle_id.collide_point(*touch.pos): print "big circle clicked"        y = (touch.y - self.center[1])        x = (touch.x - self.center[0])        calc = math.degrees(math.atan2(y, x))        self.prev_angle = calc if calc > 0 else 360+calc        self.tmp = self.angle        return super(Dial, self).on_touch_down(touch) # dispatch touch event futher    def on_touch_move(self, touch):        y = (touch.y - self.center[1])        x = (touch.x - self.center[0])        calc = math.degrees(math.atan2(y, x))        new_angle = calc if calc > 0 else 360+calc        self.angle = self.tmp + (new_angle-self.prev_angle)%360    def on_touch_up(self, touch):        Animation(angle=0).start(self)class DialApp(App):    def build(self):        return Dial()if __name__ == "__main__":    DialApp().run()


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/596543.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号