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
    21860
    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.
    • Anxietier
      Anxietier @stephen last edited by

      @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 mikael 2 Replies Last reply Reply Quote 0
      • stephen
        stephen @Anxietier last edited by

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

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

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

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

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

              mikael 2 Replies Last reply Reply Quote 0
              • mikael
                mikael @Anxietier last edited by

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

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

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

                    @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

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

                      @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 stephen 2 Replies Last reply Reply Quote 0
                      • mikael
                        mikael @Anxietier last edited by

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

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

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

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

                              @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

                              1 Reply Last reply Reply Quote 0
                              • Anxietier
                                Anxietier @Anxietier last edited by

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

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

                                  Anxietier 1 Reply Last reply Reply Quote 0
                                  • Anxietier
                                    Anxietier @Anxietier last edited by

                                    @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

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

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

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

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

                                            @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 1 Reply Last reply Reply Quote 0
                                            • First post
                                              Last post
                                            Powered by NodeBB Forums | Contributors