Forum Archive

Pure Python gestures

mikael

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.)

Demo

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, ENDED
  • location - location of the touch, or the centroid of all touches, as a scene.Point
  • no_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 includes direction, one of UP, DOWN, LEFT, RIGHT
  • on_swipe_up, on_swipe_down, on_swipe_left, on_swipe_right
  • on_pan - data includes translation, the distance from the start of the gesture, as a scene.Point. For most purposes this is better than location, as it does not jump around if you add more fingers.
  • on_pinch - data includes scale
  • on_rotate - data includes rotation in degrees, negative for counterclockwise rotation

There are also prev_translation, prev_scale and prev_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.

FrankenApps

Wow, this is very impressive. Sorry for that most likely stupid question: Is there a way to use this with a scene? I am trying to make a scene zoom+panable, but I did not manage to do it.
I wanted to use

def on_pinch(self, data):
    self.scale = data.scale 

and

def on_pan(self, data): self.frame = (data.translation.x, data.translation.y, self.width, self.height)
inside my scene class, where I would have got self.width and self.height before from ui.get_screen_size...

Maybe someone could provide me with a basic example for a zoom and panable scene?
Would be great, thanks in advance.

mikael

@FrankenApps, for scenes, it is easier to use the ObjC version, available as Gestures.py.

Panning and zooming is a bit more involved than just using the gestures, so in the same repo there is also zoompanscene.py, which you use like this:

from zoompanscene import *

class SpaceScene(ZoomPanScene):

  def setup(self):
    super().setup()

    ship = SpriteNode('spc:PlayerShip1Orange')
    ship.position = self.size / 2
    self.add_child(ship)

run(SpaceScene())
mithrendal

I am just again overwhelmed by this new clean beautiful piece of API creation from @mikael ! Thank you so much for this...

mikael

Due to popular demand (I needed it) I added edge swipe gestures to the Python version. Implement some of these methods to use them:

  • on_edge_swipe
  • on_edge_swipe_up
  • on_edge_swipe_down
  • on_edge_swipe_left
  • on_edge_swipe_right

Note: "left" means "toward left", or "from the right edge".

Also included in the same module a ZoomPanView implementation, which rotates as well! It has these constructor parameters (can be set as attributes, if you prefer):

  • pan=True, zoom=True, rotate=False
  • min_scale, max_scale
  • min_rotation, max_rotation (in degrees)

Run the file to play with the demo, or implement something like this:

class ZoomPanDemo(ZoomPanView):

  def __init__(self, **kwargs):
    self.background_color='black'
    super().__init__(
      rotate=True, 
      min_rotation=-45, max_rotation=45,
      **kwargs)

    self.crazy_text = ui.Label(
      text='Pan, zoom, rotate',
      text_color='white',
      alignment=ui.ALIGN_CENTER,
      flex='LTBR',

    )
    self.crazy_text.size_to_fit()
    self.crazy_text.center = self.bounds.center()
    self.add_subview(self.crazy_text)
Anxietier

thx for share ur code, it's really useful.
i was try to overload view class, so i can zoom imageview and drag it, like photo album, here is my problem:
when i use 2 fingers to drag imageview then pinch it, it's ok,
but when i pinch it at first, the imageview's left-top corner will be fixed for a while, as i pinch more scale, the imageview would jump under my fingers, then when i drag and pinch, the experience would be ok
i dont know am i describe it clearly, here is my demo code

```
import gestures
import ui

class MyView(ui.View):
def init(self):
self.width, self.height = ui.get_screen_size()
self.background_color = 'white'

    self.set_ui()
    self.set_label_action()

    self.label_init_center = self.label.center
    self.label_init_width = self.label.width
    self.label_init_height =  self.label.height

def set_ui(self):
    label = ui.Label()
    self.label = label
    label.text = 'Demo'
    label.border_color = 'black'
    label.border_width = 1
    label.width, label.height = 400, 400
    label.center = self.width/2, self.height/2
    self.add_subview(label)

def set_label_action(self):
    pincher = gestures.pinch(self.label, self.pinch_handler)
    panner = gestures.pan(self.label, self.pan_handler, minimum_number_of_touches=2)
    panner.together_with(pincher)

def pan_handler(self, data): 
    self.label.center = self.label_init_center + data.translation
    if self.label.text == '2 fingers are pinching':
        self.label.text += 'and panning'
    if self.label.text == 'Demo':
        self.label.text = '2 fingers are panning'
    if data.state == gestures.ENDED:
        self.label.text = 'Demo'
        self.label_init_center = self.label.center

def pinch_handler(self, data): 
    self.label.width, self.label.height = self.label_init_width * data.scale, self.label_init_height * data.scale
    if self.label.text == '2 fingers are panning':
        self.label.text += ' and pinching'
    if self.label.text == 'Demo':
        self.label.text = '2 fingers are pinching'
    if data.state == gestures.ENDED:
        self.label.text = 'Demo'
        self.label_init_width = self.label.width
        self.label_init_height = self.label.height

if name == "main":
mView = MyView()
mView.present('fullscreen', hide_title_bar = True)```

stephen

@Anxietier hey ive never dealt with gestures before but i imagin giving each movment a delegate to register separatly then get the diference of the 'onEvent' call (sorry just made that one up lol) would this posibly help? might need separate Gesture objectsbinstead of calling all from one view. not sure how that would work

Anxietier

@stephen im not sure understand whats ur meaning (not good at eng..) :
make one label, when i swipe it up\down\left\right, or drag it, or zoom it, etc.. it can do diff things
did u mean that? if so, then yes
btw, if u wanna swipe 4 directions with one handle, u can write like this:
gestures.RIGHT|gestures.LEFT|gestures.UP|gestures.DOWN

mikael

@Anxietier, can you subclass ZoomPanView instead?

If you run pygestures.py and swipe from the right, you get the demo picture that you can pan and zoom, and that seems to work ok.

The code for it is at the end of the file.

stephen

@Anxietier okay i was in a bit of a rush before and didnt visualize fully.

I have not looked at @mikael 's module yet but givin who authored it im sure ite outstsnding. that said my first post is no longer valid.. 😅😉

---

as for your original issue i went to photos and played with the pinch/pan for a bit and i noticed that if i pinched from corners ↴

good

everythingbworked fine. BUT if one finger wasnt close enough to corner ↴

bad

the gester would act as though it had constraints.

if i understand your problem then possibly these are similarbin effect and that should mean increasing the "touch" target rect size would help prevent this?

Note: inused photos App givin u wanted to Mock functionality ☺️🤓

Anxietier

@mikael hi, i tested zoomPanDemo, and it works fine, thanks for advice.
btw, im trying to make an effect that when i drag down PanView, it can make me feel damping, as my finger move away, it can get back to origin pos, feel like looking website, drag down for refresh
could you provide any idea for that? thank you

Anxietier

@stephen thx for reply
ive tryed mikael's zoomPanDemo, add it to my main View, and it works fine, so the issue is on mine :(
i would subclass ZoomPanView, and try it :D

stephen

@Anxietier no problem. i looked over the moduleand it looks like all uwould need to do is run a ui.animate transgorm back yto orignal psition.if touch doesnt store the original long enough (it should) you can also store this in a var. should be straight forward butd have to run through this module a bit beore i can give an example.

Anxietier

@stephen
yeah, i made a demo, i subclass ui.view, add ui,imageview to show image, and i def a method to fix imageview_size to view_size, include view_center, when i drag down imageview and leave my finger, it can move back to origin pos,
but, i need an experience, i'd try to describe it:
'' when i drag it down shortly, imageview would move down follow my finger,
when my finger drag long way, like from top screen to bottom, the imagview wouldnt follow my finger, feel like dragging a strong
spring ''
i try to find a math function to let imageview and fingers' trave non-linear, but i failed, so ... u know :P

stephen

@Anxietier 🤓 ah math now i can help u. give me a few min to write up a friction/drag method for you

mikael

@Anxietier, please see below for something you can adjust to your needs.

I recommend and use scripter, because it makes the animations easy and includes the ease functions.

import ui
import scripter

v = ui.View(background_color='black')

class Pulled(ui.View):

    straight_pull = 100
    slowing_pull = 100

    def touch_began(self, t):
        self.start_y = ui.convert_point(t.location, from_view=self).y

    def touch_moved(self, t):
        delta_y = (
            ui.convert_point(t.location, from_view=self).y - 
            ui.convert_point(t.prev_location, from_view=self).y)
        if self.y < self.straight_pull:
            self.y += delta_y
            return
        if self.y < (self.straight_pull + self.slowing_pull):
            diff = self.y-self.straight_pull
            diff_fraction = diff/self.slowing_pull
            effective = 1 - min(1, diff_fraction) ** 2
            self.y += delta_y * effective

    def touch_ended(self, t):
        scripter.y(self, 0, ease_func=scripter.ease_out)

v.add_subview(Pulled(
    frame=v.bounds,
    flex='WH',
    background_color='green'))

v.present('fullscreen')
Anxietier

@mikael
thank you, when i ran ur code, it post me an error:
scripter.y(self, 0, ease_func=scripter.ease_out)
module 'scripter' has no attribute 'y'

then i check scripter.py, i only found the vector_class has a method called y(...)
im confused~

Anxietier

@Anxietier @mikael
oh, never mind, i restarted app, now its wording fine :D

mikael

@Anxietier, and a version with a revealed label and a refresh trigger:

import ui
import scripter

v = ui.View(background_color='black')

class Pulled(ui.View):

    straight_pull = 30
    slowing_pull = 80

    def touch_began(self, t):
        self.start_y = ui.convert_point(t.location, from_view=self).y

    def touch_moved(self, t):
        delta_y = (
            ui.convert_point(t.location, from_view=self).y - 
            ui.convert_point(t.prev_location, from_view=self).y)
        if self.y < self.straight_pull:
            self.y += delta_y
            return
        if self.y < (self.straight_pull + self.slowing_pull):
            diff = self.y-self.straight_pull
            diff_fraction = diff/self.slowing_pull
            try:
                self.reveal_func(diff_fraction)
            except AttributeError: pass
            effective = 1 - min(1, diff_fraction) ** 2
            self.y += delta_y * effective


    def touch_ended(self, t):
        scripter.y(self, 0, ease_func=scripter.ease_out)
        try:
            self.reveal_func(0)
        except AttributeError: pass
        if self.y > self.straight_pull:
            self.trigger_func()

label = ui.Label(text='Release to refresh',
    text_color='white',
    flex='LBR',
    alpha=0,
)
label.size_to_fit()
label.center = v.bounds.center()
label.y = 8
v.add_subview(label)

def reveal(fraction):
    label.alpha = fraction

def trigger():
    scripter.hide(label)
    print('refresh')

v.add_subview(Pulled(
    reveal_func=reveal,
    trigger_func=trigger,
    frame=v.bounds,
    flex='WH',
    background_color='green'))

v.present('fullscreen')
mikael

@Anxietier, oh, you can throw away the touch_began method, it’s from an earlier version.

Anxietier

@mikael thank you
and if i want to bind it with gestures, what should i do
i mean is there any conflict between gestures and touch
what i want to make is, i can use twin fingers to zoom and move picture(imageview or subclass zoompanview, later is better i guess), and when i move picture, move space and scale would be limited, as i leave my fingers, picture's edge should be forced move to superview's edge

Anxietier

@mikael ah, dont bother
i didnt read doc carefully(perhaps wrong word :P), now i can bind them together, thanks for ur help, ur codes are really amazing and helpful :D

mikael

@Anxietier, little optimization of the ”feel” and code simplification.

import ui
import scripter

v = ui.View(background_color='black')

class Pulled(ui.View):

    trigger_distance = 30
    pull_distance = 80

    def touch_moved(self, t):
        delta_y = (
            ui.convert_point(t.location, from_view=self).y - 
            ui.convert_point(t.prev_location, from_view=self).y)
        diff_fraction = self.y/self.pull_distance
        try:
            self.reveal_func(diff_fraction)
        except AttributeError: pass
        self.y += delta_y * (1 - min(0.9, diff_fraction) ** 2)
        self.y = max(0, self.y)

    def touch_ended(self, t):
        scripter.y(self, 0, ease_func=scripter.ease_out)
        try:
            self.reveal_func(0)
        except AttributeError: pass
        if self.y > self.trigger_distance:
            self.trigger_func()

label = ui.Label(text='Release to refresh',
    text_color='white',
    flex='LBR',
    alpha=0,
)
label.size_to_fit()
label.center = v.bounds.center()
label.y = 8
v.add_subview(label)

def reveal(fraction):
    label.alpha = fraction

def trigger():
    scripter.hide(label)
    print('refresh')

v.add_subview(Pulled(
    reveal_func=reveal,
    trigger_func=trigger,
    frame=v.bounds,
    flex='WH',
    background_color='green'))

v.present('fullscreen')
stephen

!!! Removed by poster to not confuse future readers with incorrect Code Examples.. 🤓😉

Anxietier

@mikael
thanks for help, i make it
but there's another problem showed up:
i make a main view, then i subclass zoomPanView and add to mainView , after that i add imageView as zoomPanView's subVIew, like what zoomPanDemo did, when i overwrite on_pan method and test it, data.began and data.changed can give me right value, but data.ended not, i dont know why

Anxietier

@stephen thanks for help!
im would try to understand ur code, im not good in math though :(
btw, i tried mikael's code, and it works fine to me :P

Anxietier

@Anxietier said:

@mikael
thanks for help, i make it
but there's another problem showed up:
i make a main view, then i subclass zoomPanView and add to mainView , after that i add imageView as zoomPanView's subVIew, like what zoomPanDemo did, when i overwrite on_pan method and test it, data.began and data.changed can give me right value, but data.ended not, i dont know why

one more thing, i overwrote on_pinch too, and i printed data.ended, and got right value, maybe two gesture's ended flag conflict ?

mikael

@Anxietier, thanks for catching this. Pan end event was not properly handled, fixed now in the version on the repo.

Anxietier

@Anxietier said:

@Anxietier said:

@mikael
thanks for help, i make it
but there's another problem showed up:
i make a main view, then i subclass zoomPanView and add to mainView , after that i add imageView as zoomPanView's subVIew, like what zoomPanDemo did, when i overwrite on_pan method and test it, data.began and data.changed can give me right value, but data.ended not, i dont know why

one more thing, i overwrote on_pinch too, and i printed data.ended, and got right value, maybe two gesture's ended flag conflict ?

i resolved this issue
i subclass both view and GestureMixin as new ZoomPanView, then i overwrite touch_ended, so that when i subclass new ZoomPanView i can edit touch_ended,
high encapsulation not always convenient XD

Anxietier

@mikael said:

@Anxietier, thanks for catching this. Pan end event was not properly handled, fixed now in the version on the repo.

expecting for new version (👍 ͡❛ ͜ʖ ͡❛)👍

mikael

@Anxietier, is there a specific reason why you are using the Python version instead of the native iOS gestures? Just curious, because I no longer remember why I wrote pygestures. 😁 ... Probably just because I wanted to see if I could do it.

Anxietier

@mikael
well, im not good at math, i tried use touch_began\move\ended, and surely i can make simple function, but as it became complex, my brain be in chaos, XD

mikael

@Anxietier, I meant choosing between gestures.py (wrapper around iOS native gestures) and pygestures.py (pure Python gestures built on top of the ui.View.touch_x method ”primitives”.

Your answer leads me to guess you might not have been aware of the first option.

Anxietier

@mikael oh, i did try gesture.py, got some problems, and you suggested me subclass ZoomPanView instead for my need, then i did and i find it's interesting, so ~~ :))

@mikael said:

@Anxietier, can you subclass ZoomPanView instead?

If you run pygestures.py and swipe from the right, you get the demo picture that you can pan and zoom, and that seems to work ok.

The code for it is at the end of the file.

mikael

@Anxietier, oh, and I thought you were already using the python version, given the title of this discussion thread... 😄

stephen

@Anxietier

do you still need help with drag/friction algorithm ?

Anxietier

@stephen
thank you, maybe not, i did solve this problem (probably not yet)
thanks for your help :)

Anxietier

@mikael
haha, I was trying to make a zoom demo, but the effect is not good, so I googled key words "pythonista 2 finger zoom", then luckily found this XD

stephen

@Anxietier

Awesome!

ok well il still working on it because parts of the process are in my todo fo mainproject. ill post finished codebwith coments here when finished

Anxietier

@stephen
good luck ! (っ^▿^)۶🍸🌟🍺٩(˘◡˘ )

stephen

@Anxietier
thanks, i noticed you like those ٩(•̮̮̃•̃)۶ faces. idk if you type them everytime but i "buffed" the keyboard example script and gave it more functionality here you will be able to select them from a list that you can add to yourself heres a couple thats included.

٩(•̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ ٩(̃•̃•̃)۶ ٩(͡๏̯͡๏)۶ ٩(-̮̮̃•̃)۶ ε(̮̮̃•̮̮̃̃•̃)з (๏̯͡๏) |=_=|

Anxietier

@stephen
haha, I would try it

stephen

@mikael i usually do great at dealing with numbers... after a couple days of this i seem to have made a circle back to where i started... it might be that i dont understand Sigma or i may be using the wrong function all together... i even tried generating a logarithmic table then iterating the values to get drag to stop movment lol. any chance you @cvp @JonB or @ccc know a non built-in way to do this? here is where my testing code as it was when i stopped to post this... sorry the codes messy..


import numpy as np
import math
from scene import *
w, h=get_screen_size()

def Distance(pos1, pos2):
    x1, y1=pos1
    x2, y2=pos2
    dx, dy=x1-x2, y1-y2
    return math.sqrt(dx*dx + dy*dy)

def Force(pos1, pos2, k):
    x1, y1=pos1
    x2, y2=pos2
    return Distance(pos1, pos2)*k#/1*(1+np.exp(1))**k

def Direction(pos1, pos2):
    x1, y1=pos1
    x2, y2=pos2
    dx=(x1-x2)/Distance(pos1,pos2)
    dy=(y1-y2)/Distance(pos1,pos2)
    return Point(dx, dy)

class main (Scene):
    def setup(self):
        self.isDragging = False
        self.drag_T=0
        self.i=0

        self.touch_loc=(0, 0)
        self.ticks=0
        self.tick_rate=0.01
        self.origin_pos=Point(w/2, 64)

        self.subject=SpriteNode(
            texture=None, color=(0.0, 1.0, 0.0), size=Size(64, 64),
            position=self.origin_pos, anchor_point=(0.5, 0.5), parent=self)
        self.sub_data=LabelNode(
            text=f'(◕_◕)', font=('Menlo', 22), anchor_point=(0.5, 0.5),
            color=(0.0, 0.0, 0.0, 1.0), parent=self.subject)
        self.data=LabelNode(
            text="", font=('Menlo', 14), anchor_point=(0.0, 0.0), color=(1.0, 1.0, 1.0),
            position=Point(150,512), size=Size(256, 512), parent=self)


    def update(self):
        w, h=get_screen_size()
        self.i+=self.dt
        self.ticks+=1
        self.drag_T= (self.drag_T+1) if self.isDragging else 0
        d_o=Distance(self.subject.position, self.origin_pos)
        d_s=Distance(self.subject.position, self.touch_loc)
        d_t=Distance(self.origin_pos, self.touch_loc)
        max=Distance((w/2, h/3), self.subject.position)
        target_dir=Direction(self.touch_loc, self.subject.position)
        f=Force(self.origin_pos, self.touch_loc, self.drag_T)
        damp=(1/(1+np.exp(-max)))


        if self.isDragging:
            w, h=get_screen_size()
            x, y=self.subject.position
            xx, yy=self.touch_loc
            tx, ty=target_dir
            if max > 1:
                self.subject.position=(x+tx, y+ty+max)

        self.DataUpdate(d_t, d_s, d_o, max, f, target_dir)

        self.i=0
    def DataUpdate(self, d_t_o, d_s_t, d_s_o, max, f, t_dir):
        self.data.text= (f'Ticks: {self.ticks}\n'+
                        f'Dist from Max {max}\n'+
                        f'Force/Friction: {f}\n'+
                        f'Drag Time: {self.drag_T}\n'+
                        f'isDragging: {self.isDragging}\n'+
                        f'Target Direction: {t_dir}\n'+
                        f'Subject position: {self.subject.position}\n'+
                        f'Touch location: {self.touch_loc}\n'+
                        f'Origin: {self.origin_pos}\n\n'+
                        f'Distance - Touch→Origin: {d_t_o}\n'+
                        f'Distance - Subject→Origin: {d_s_o}\n'+
                        f'Distance - subject→Touch: {d_s_t}\n')

    def touch_began(self, touch):
        self.isDragging = True
        self.touch_loc=touch.location

    def touch_moved(self, touch):
        tx, ty=touch.location
        sx, sy=self.subject.position
        self.touch_loc=touch.location

    def touch_ended(self, touch):
        self.isDragging = False
        #self.touch_loc=(0, 0)
        #self.direction=(0, 0)
        self.drag_T=0
        self.subject.position=self.origin_pos
run(main())


cvp

@stephen I'm sincerely sorry but, perhaps/obviously due to my poor English, I don't understand what you want to do with this script.

cvp

@stephen first remark, setting tx,ty,sx,sy is useless, isn't it?

    def touch_moved(self, touch):
        tx, ty=touch.location
        sx, sy=self.subject.position
        self.touch_loc=touch.location 
stephen

@cvp yes very much so in is what i posted. when i test i use alot of exlusive calls to insure control to eliminate inclusive bug or errors then when i achieve outcome expected i remove exclusive properties one at a time . just a metegation process i developed over the years. i should have mentioned this im sorry for the confusion. lol ☺️ the one your speaking of used to be connected to instance variables that i already removed lol kinda what i meant when i said sorry for the messy code lol

also my goal was to creat a drag dampener. for example dragging an object from the bottom of the screen thats constrained to half screen size for travel. nd object speed slows as it aproaches max distance. similar to a vehicle comng to a stop at a trafic light..

cvp

@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 🙄

stephen

@cvp

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.

cvp

@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...

mikael

@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”.

stephen

@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.

Drizzel

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')
stephen

@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 of class Delegate instead of a global function.

stephen

@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?

mikael

@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')
Drizzel

@mikael thanks, that works. I just change the cell‘s background color to lightgrey for visual feedback

Drizzel

@stephen True, I corrected it :) The code was a bit rushed 🤷‍♂️

stephen

awesome 😎🤓😊

mikael

@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')
Anxietier

hi, i found another bug, after i use 5 fingers to switch app and get back, multi-touch would be not working

cvp

@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).

cvp

@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
mikael

@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)
cvp

@mikael thanks, I'll try

Édit: done, ok
Re-Edit: marvelous module

itibettravel

@stephen said:

Awesome!

Awesome!