Why does this flicker?
I've written some incredibly basic code for a custom view class that can be dragged/dropped.
class DragDrop(ui.View): def touch_moved(self, touch): tx, ty = touch.location self.x,self.y = tx-self.frame/2, ty-self.frame/2 v=ui.View() dnd = DragDrop(frame=(100,100,100,100)) dnd.background_color=(0,0,0) v.add_subview(dnd) v.present(hide_title_bar=1)
However when I drag an object, its position flickers between two positions. Why is this, and how can I fix it?
Touch position changes in the
touch_movedmethod between calls (yeah bear with me that ones kinda obvious) but what I mean is that moving your finger from top left to bottom right on each move there is two changes for some reason. (0,0) and (0,-1) simultaneously hence the flicker.
This is shown simply by printing to touch coordinats.
print tx, ty
Solution: find delta x and delta y (ie the change from the current position and the last position)
class DragDrop(ui.View): def touch_moved(self, touch): cx, cy = touch.location ox, oy = touch.prev_location tx, ty = ox-cx, oy-cy print tx, ty self.x -= tx self.y -= ty