omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    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.


    Pure Python gestures

    Pythonista
    8
    65
    21906
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • stephen
      stephen @Drizzel last edited by 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.

      Drizzel 1 Reply Last reply Reply Quote 1
      • stephen
        stephen @Drizzel last edited by

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

        1 Reply Last reply Reply Quote 0
        • mikael
          mikael @Drizzel last edited by

          @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')
          
          1 Reply Last reply Reply Quote 3
          • Drizzel
            Drizzel last edited by

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

            mikael 1 Reply Last reply Reply Quote 0
            • Drizzel
              Drizzel @stephen last edited by

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

              1 Reply Last reply Reply Quote 1
              • stephen
                stephen last edited by

                awesome 😎🤓😊

                1 Reply Last reply Reply Quote 0
                • mikael
                  mikael @Drizzel last edited by 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')
                  
                  cvp 2 Replies Last reply Reply Quote 0
                  • Anxietier
                    Anxietier last edited by

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

                    1 Reply Last reply Reply Quote 1
                    • cvp
                      cvp @mikael last edited by

                      @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 1 Reply Last reply Reply Quote 0
                      • cvp
                        cvp @mikael last edited by

                        @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
                        
                        1 Reply Last reply Reply Quote 1
                        • mikael
                          mikael @cvp last edited by 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 1 Reply Last reply Reply Quote 2
                          • cvp
                            cvp @mikael last edited by cvp

                            @mikael thanks, I'll try

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

                            1 Reply Last reply Reply Quote 0
                            • itibettravel
                              itibettravel last edited by

                              This post is deleted!
                              1 Reply Last reply Reply Quote 0
                              • First post
                                Last post
                              Powered by NodeBB Forums | Contributors