Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
long touches and swipes
-
Can the gurus enlighten as how to do? I presume timers and touch.ids are involved but if you have a snippet or two to lead the way, would be much appreciated.
-
I have a few examples in uicomponents.
PopupButton
implements a long touch action.There are probably lots of different types of behavior, this was trying to emulate the pythonista keyboard, where a long touch brings up another key row.
In the simplest form,
touch_began
starts a timer (you can just useui.delay
). The function that gets called sets a flag indicating long touch is active. That function could also do some other action -- like popping up a menu in this case.touch_ended
callsui.cancel_delays()
(to cancel any pending long touches in the case of a "short touch") and might take a different action depending on whether a long touch was active or not.touch_moved
could cancel the delay (if you only want a stationary long touch to activate). What made sense for this case was that small moves were still allowed to trigger a long touch, as long as the touch was still on the component when the timer went off (hence thetouched
flag, which also causes the draw method to highlight or unhighlight the component)For simple swipe actions, you could simply look at the
touch.prev_location
and compare totouch.location
in order to figure out the swipe direction, insidetouch_moved
. However if you want to do any sort of filtering on the total amount the finger has moved, you need to keep track of touches on your own (bytouch_id
if multitouch is enabled)ZoomView shows an example of a multi-touch enabled view. Click the new window button; two-finger drag on the border move the window, pinching causes the window to zoom (i.e. touch two points on border and drag apart)
Note that most fancy touch events are completely broken in
present('panel')
mode, since you only get onetouch_moved
event, at which point things try to scroll back to the editor.ScrollView
andWebView
don't have this limitation, and so some tricky things can be done withScrollView
to simulate swipe events. For an example,In stash, we implemented a scrollview which when swiping moves the cursor. Basically,
scrollview_did_scroll
grabsscrollview.content_offset
, (keeping a running sum, perhaps), then resetscontent_offset back to (0,0,0,0)
, so that the scroll never actually gets rendered. -
get this when I "long touch" one of the buttons:
Traceback (most recent call last): File "/var/mobile/Containers/Data/Application/3469D264-D1AC-451E-9E4A-B3E38AD33B7F/Documents/uicomponents/PopupButton.py", line 177, in touch_moved self.longtouchcleanuptimer.cancel() AttributeError: 'PopupButton' object has no attribute 'longtouchcleanuptimer'
Running 1.6
Changing from 'panel' to 'sheet' presentation locks things up and requires a restart.
-
Whoops, I had a try/catch not checked in. Update has been pushed.