Note that there are some explanatory texts on larger screens.

plurals
  1. PORotating an object on a touch event in kivy
    text
    copied!<p>I'm working on making a circle that will spin kind of like a large dial. Currently, I have an arrow at the top to show which direction the dial is facing. I'd like its behavior to be kind of like an old timey rotary phone, such that while your finger/cursor is down you can rotate it, but it'll (slowly) yank back to top after you let go.</p> <p>Here's what my object looks like:</p> <p><img src="https://i.stack.imgur.com/aW7YM.png" alt="enter image description here"></p> <p>And here's my code:</p> <pre><code>#!/usr/bin/kivy import kivy kivy.require('1.7.2') import math from random import random from kivy.app import App from kivy.uix.widget import Widget from kivy.uix.gridlayout import GridLayout from kivy.uix.anchorlayout import AnchorLayout from kivy.uix.relativelayout import RelativeLayout from kivy.graphics import Color, Ellipse, Rectangle class MinimalApp(App): title = 'My App' def build(self): root = RootLayout() return(root) class RootLayout(AnchorLayout): pass class Circley(RelativeLayout): angle = 0 def on_touch_down(self, touch): ud = touch.ud ud['group'] = g = str(touch.uid) return True def on_touch_move(self, touch): ud = touch.ud # print(touch.x, 0) # print(self.center) # print(0, touch.y) # print(touch.x - self.center[0], touch.y - self.center[1]) y = (touch.y - self.center[1]) x = (touch.x - self.center[0]) calc = math.degrees(math.atan2(y,x)) angle = calc if calc &gt; 0 else (360 + calc) print(angle) def on_touch_up(self, touch): touch.ungrab(self) ud = touch.ud return True if __name__ == '__main__': MinimalApp().run() </code></pre> <p>And the kv:</p> <pre><code>#:kivy 1.7.2 #:import kivy kivy &lt;RootLayout&gt;: anchor_x: 'center' # I think this /is/ centered anchor_y: 'center' canvas.before: Color: rgba: 0.4, 0.4, 0.4, 1 Rectangle: pos: self.pos size: self.size Circley: anchor_x: 'center' # this is /not/ centered. anchor_y: 'center' canvas.before: PushMatrix Color: rgba: 0.94, 0.94, 0.94, 1 Rotate: angle: self.angle axis: 0, 0, 1 origin: self.center Ellipse: source: 'arrow.png' 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) Label: text: unicode(self.size) # this is /not/ appearing color: 1,0,0,1 canvas.after: PopMatrix </code></pre> <p>Parts of that are borrowed from the kivy touchtracer demo, and <a href="https://stackoverflow.com/questions/17674736/kivy-how-to-rotate-a-picture">from this SO question</a>.</p> <p>You can see I have a calculation that is correctly printing the angle between the origin of the circle and the touch event (not sure how this'll respond to multiple fingers, haven't thought that far through), but not sure how to integrate this into a "spinning" feedback event in the interface.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload