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.


    If the cursor is hover a view.

    Pythonista
    2
    12
    153
    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.
    • Raymond
      Raymond last edited by

      is there a built-in way to detect if the mouse cursor is on hover a view ?
      if not, can i do it with objc_util

      Like this on swift UIkit:

      class MyView: UIView {
          override func hover(_ hover: Bool) {
              super.hover(hover)
      
              if hover {
                  // This code will execute when the mouse cursor enters the view.
              } else {
                  // This code will execute when the mouse cursor leaves the view.
              }
          }
      }
      
      cvp 1 Reply Last reply Reply Quote 0
      • cvp
        cvp @Raymond last edited by

        @Raymond should be possible, see https://developer.apple.com/documentation/uikit/uihovergesturerecognizer

        Raymond 1 Reply Last reply Reply Quote 0
        • Raymond
          Raymond @cvp last edited by

          @cvp Can you write an example with it ?

          cvp 4 Replies Last reply Reply Quote 0
          • cvp
            cvp @Raymond last edited by cvp

            @Raymond Not today. I guess @mikael could easily modify its gestures module to support this UIHoverGestureRecognizer recognizer.

            If nobody is available, I could try tomorrow in the afternoon (Belgian time) but I don't have a mouse or a pencil. Or perhaps I could use the mouse of my Mac.

            1 Reply Last reply Reply Quote 0
            • cvp
              cvp @Raymond last edited by cvp

              This post is deleted!
              1 Reply Last reply Reply Quote 0
              • cvp
                cvp @Raymond last edited by cvp

                @Raymond tried this code successfully

                import ui
                from gestures import *
                
                UIHoverGestureRecognizer = ObjCClass('UIHoverGestureRecognizer')
                
                class MyView (ui.View):
                	@on_main_thread
                	def __init__(self, *args, **kwargs):
                		ui.View.__init__(self, *args, **kwargs)
                		self.background_color = 'lightgray'
                		
                		self.v = ui.View()
                		self.v.frame = (10,10,100,100)
                		self.v.background_color = 'white'
                		self.add_subview(self.v) 
                		
                		handler = UIGestureRecognizerDelegate(UIHoverGestureRecognizer, self.v, self.hover)
                		
                	def hover(self, data):
                		print(data)
                
                def main():
                	mv = MyView(frame=(0, 0, 500, 500))
                	mv.present('sheet')	
                	
                if __name__ == '__main__':
                	main()
                
                1 Reply Last reply Reply Quote 0
                • cvp
                  cvp @Raymond last edited by cvp

                  @Raymond or try with

                  	def hover(self, data):
                  		if data.state == 1: # began
                  			data.view.background_color = 'red'
                  		elif data.state == 2: # changed
                  			pass
                  		elif data.state == 3: # ended
                  			data.view.background_color = 'white'
                  		#print(data)
                  
                  Raymond 1 Reply Last reply Reply Quote 0
                  • Raymond
                    Raymond @cvp last edited by ccc

                    @cvp i am using pythonista beta, the new one.

                    The code:

                    import ui
                    from objc_util import *
                    
                    UIHoverGestureRecognizer = ObjCClass('UIHoverGestureRecognizer')
                    
                    class MyView (ui.View):
                    	@on_main_thread
                    	def __init__(self, *args, **kwargs):
                    		ui.View.__init__(self, *args, **kwargs)
                    		self.background_color = 'lightgray'
                    		
                    		self.v = ui.View()
                    		self.v.frame = (10,10,100,100)
                    		self.v.background_color = 'white'
                    		self.add_subview(self.v) 
                    		
                    		handler = UIGestureRecognizerDelegate(UIHoverGestureRecognizer, self.v, self.hover)
                    		
                    	def hover(self, data):
                    		if data.state == 1: # began
                    			data.view.background_color = 'red'
                    		elif data.state == 2: # changed
                    			pass
                    		elif data.state == 3: # ended
                    			data.view.background_color = 'white'
                    		#print(data)
                    
                    
                    
                    v = MyView()
                    

                    There is this error with your code:

                    Exception ignored on calling ctypes callback function: <function OMMainThreadDispatcher_invoke_imp at 0x1152d0160>
                    Traceback (most recent call last):
                      File "/var/containers/Bundle/Application/534A53C8-B40F-4EBF-971B-5FBA04EFD7E4/Pythonista3.app/Frameworks/Py3Kit.framework/pylib/site-packages/pythonista/objc_util.py", line 1066, in OMMainThreadDispatcher_invoke_imp
                        retval = func(*args, **kwargs)
                      File "/private/var/mobile/Containers/Shared/AppGroup/48E83FD5-AA04-45A2-86D9-75BE4BCA2B89/Pythonista3/Documents/T.py", line 17, in __init__
                        handler = UIGestureRecognizerDelegate(UIHoverGestureRecognizer, self.v, self.hover)
                    NameError: name 'UIGestureRecognizerDelegate' is not defined
                    
                    cvp 2 Replies Last reply Reply Quote 0
                    • cvp
                      cvp @Raymond last edited by cvp

                      @Raymond I also use the new beta but you did change my code.
                      I don't import objc_util but gestures, the module I described some posts above.
                      You have to download it and install in site-packages

                      gestures.py

                      1 Reply Last reply Reply Quote 0
                      • cvp
                        cvp @Raymond last edited by cvp

                        @Raymond If you don't like to import gestures, I could try to extract necessary code... But not for today

                        Raymond 1 Reply Last reply Reply Quote 0
                        • Raymond
                          Raymond @cvp last edited by

                          @cvp No it works!! Thank you very much !!

                          cvp 1 Reply Last reply Reply Quote 0
                          • cvp
                            cvp @Raymond last edited by

                            @Raymond 👍

                            1 Reply Last reply Reply Quote 0
                            • First post
                              Last post
                            Powered by NodeBB Forums | Contributors