Pure Python gestures
-
Python gesture implementation on Github for those situations where you cannot or do not want to use the ObjC gestures.
Simple usage example:
import pygestures class MyTouchableView(pygestures.GestureView): def on_swipe(self, data): if data.direction in (data.UP, data.DOWN): print('I was swiped vertically')
Run the file as-is to play around with the gestures. (Green circles track your touches, crosshairs show the centroid, red circle reflects pan, pinch and rotation.)
In your subclass, implement any or all the methods below to handle gestures. All methods get an information object with attributes including:
state
- one of BEGAN, CHANGED, ENDEDlocation
- location of the touch, or the centroid of all touches, as a scene.Pointno_of_touches
- use this if you want to filter for e.g. only taps with 2 fingers
Methods:
on_tap
on_long_press
on_swipe
- data includesdirection
, one of UP, DOWN, LEFT, RIGHTon_swipe_up
,on_swipe_down
,on_swipe_left
,on_swipe_right
on_pan
- data includestranslation
, the distance from the start of the gesture, as a scene.Point. For most purposes this is better thanlocation
, as it does not jump around if you add more fingers.on_pinch
- data includesscale
on_rotate
- data includesrotation
in degrees, negative for counterclockwise rotation
There are also
prev_translation
,prev_scale
andprev_rotation
, if you need them.If it is more convenient to you, you can inherit GestureMixin together with ui.View or some other custom view class. In that case, if you want to use e.g. rotate, you need to make sure you have set
multitouch_enabled = True
.
-
@stephen ok, understood. I'm really not a specialist of games or scene but I wonder if there are no functions for the physical aspect of the movements.
Hoping other guys will help you. And sorry, I shouldn't have doubted 🙄
-
there are animations and i beleive interpolation functions i could use and i, sure they work amazingly 🙃 but i dont learn much using premade. i like to try and understand whats going on even behind the scenes. then once i understand then ill use builtin "shortcuts". i guess i like to make things as dificult as possible lol at least thats what my ex-wife would say 😅😂😅
thank you for checking it out tho its much appritiated. and just fyi the only diference from games and other types of programs is graphical programing mainly. and resource managment. i use scene module mainly for touch input controls and i like the way it implements the eventloop. i could just use my own loop and objc for touch input. but i am slowly replacing parts of scene with objc code cuz it has been improving performance.
-
@stephen I can understand that. Due to my experience of (very) old guy using (very) old tools, I like to do all myself and I never have used Scene. I also prefer do animations my self in update method.
Good luck and have fun...
-
@stephen, the original request for ”pull to refresh” was covered earlier with one line of ”math”. What are you trying to achieve here? Spring force? Coulomb effect?
If you like
scene
and like to play with forces, you could look at SpriteKit - but that would not be ”rolling your own”.
-
@mikael its ok but yes i was trying to acheive a spring like constraint. but ill go ahead a put it to the side for now. thanks for the reply.
-
I‘m trying to double tap on a TableViewCell without actually selecting it, but it should still be selectable on a single tap. How would I go about doing this?
Let’s assume I have this code:
import ui import gestures def double_tap(data): print('double tapped row', data.view.row) def tap(data): print('tapped', data.view.row) def long_press(data): print('long pressed', data.view.row) def tableview_cell_for_row(tableview, section, row): data = tableview.data_source.items[row] cell = ui.TableViewCell('subtitle') cell.text_label.text = data cell.row = row gestures.doubletap(cell, double_tap) #gestures.tap(cell, tap, number_of_touches_required = 1) #gestures.long_press(cell, long_press, minimum_press_duration = .2) return cell class Delegate(): def tableview_did_select(self, tableview, section, row): print('selected row', row) tv = ui.TableView() tv.delegate = Delegate() tv.data_source = ui.ListDataSource([str(x) for x in range(5)]) tv.data_source.delete_enabled = False tv.data_source.tableview_cell_for_row = tableview_cell_for_row tv.frame = (tv.frame[0], tv.frame[1], 400, 400) tv.present(style = 'sheet')
-
@Drizzel said:
I‘m trying to double tap on a TableViewCell without actually selecting it, but it should still be selectable on a single tap. How would I go about doing this?
Let’s assume I have this code:
import ui import gestures def double_tap(data): print('double tapped row', data.view.row) def tap(data): print('tapped', data.view.row) def long_press(data): print('long pressed', data.view.row) def tableview_cell_for_row(tableview, section, row): data = tableview.data_source.items[row] cell = ui.TableViewCell('subtitle') cell.text_label.text = data cell.row = row gestures.doubletap(cell, double_tap) #gestures.tap(cell, tap, number_of_touches_required = 1) #gestures.long_press(cell, long_press, minimum_press_duration = .2) return cell class Delegate(): def tableview_did_select(self, tableview, section, row): print('selected row', row) tv = ui.TableView() tv.delegate = Delegate() tv.data_source = ui.ListDataSource([str(x) for x in range(5)]) tv.data_source.delete_enabled = False tv.data_source.tableview_cell_for_row = tableview_cell_for_row tv.frame = (tv.frame[0], tv.frame[1], 400, 400) tv.present(style = 'sheet')
i believe you might wand
def tableview_cell_for_row(tableview, section, row):
as a method ofclass Delegate
instead of a global function.
-
@Drizzel
im sure there is a much simpler way but...why not have a state checker give x time to look for a second tap before executing functionality for a single tap? possibly a decorator?
-
@Drizzel, below works to separate the two gestures, but we lose the visual feedback for the tap selection, you would need to implement that yourself.
Better would be to find the built-in tap and give our doubletap preference over it, but I could not (yet...?) find it on the tableview, cell, content view, or the label.
import ui import gestures def double_tap(data): print('double tapped row', data.view.row) def tap(data): print('tapped', data.view.row) def tableview_cell_for_row(tableview, section, row): data = tableview.data_source.items[row] cell = ui.TableViewCell('subtitle') cell.selectable = False cell.text_label.text = data cell.row = row doubler = gestures.doubletap(cell, double_tap) tapper = gestures.tap(cell, tap) doubler.before(tapper) return cell class Delegate(): def tableview_did_select(self, tableview, section, row): print('selected row', row) tv = ui.TableView(allows_selection=False) tv.delegate = Delegate() tv.data_source = ui.ListDataSource([str(x) for x in range(5)]) tv.data_source.delete_enabled = False tv.data_source.tableview_cell_for_row = tableview_cell_for_row tv.present('fullscreen')
-
@mikael thanks, that works. I just change the cell‘s background color to lightgrey for visual feedback
-
@stephen True, I corrected it :) The code was a bit rushed 🤷♂️
-
awesome 😎🤓😊
-
@Drizzel, here’s a version that supports both the doubletap and regular row selection.
import ui import gestures def double_tap(data): print('double tapped row', data.view.row) def tableview_cell_for_row(tableview, section, row): data = tableview.data_source.items[row] cell = ui.TableViewCell('subtitle') cell.text_label.text = data cell.row = row doubler = gestures.doubletap(cell, double_tap) doubler.recognizer.delaysTouchesBegan = True return cell class Delegate(): def tableview_did_select(self, tableview, section, row): print('selected row', row) tv = ui.TableView() tv.delegate = Delegate() tv.data_source = ui.ListDataSource([str(x) for x in range(5)]) tv.data_source.delete_enabled = False tv.data_source.tableview_cell_for_row = tableview_cell_for_row tv.present('fullscreen')
-
hi, i found another bug, after i use 5 fingers to switch app and get back, multi-touch would be not working
-
@mikael Do you think it would be possible to use you gestures module on an objectiveC object?
I want to add gestures on a SceneView object, but functions like tap require their view parameter as an object having objc_instance property (see UIGestureRecognizerDelegate init).
-
@mikael I solved it with
def scene_view_tap(self,sender): location = self.recognizer.locationInView_(self.scene_view) node = self.scene_view.hitTest_options_(location,None) print(node) . . . self.gesture_recognizer_target = ui.Button() self.gesture_recognizer_target.action = self.scene_view_tap UITapGestureRecognizer = ObjCClass('UITapGestureRecognizer') self.recognizer = UITapGestureRecognizer.alloc().initWithTarget_action_(self.gesture_recognizer_target, sel('invokeAction:')).autorelease() scene_view.addGestureRecognizer_(self.recognizer) self.scene_view = scene_view
-
@cvp, forgot to advertise earlier that the latest version of gestures in the ui3 module accepts ObjC views in addition to ui module views.
from ui3.gestures import * tap(objc_view, handler)
-
@mikael thanks, I'll try
Édit: done, ok
Re-Edit: marvelous module
-
This post is deleted!last edited by