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.


    Use objc_util functions to draw on a `ui.View`

    Pythonista
    3
    4
    3048
    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.
    • Webmaster4o
      Webmaster4o last edited by

      Can I draw on a ui.View with ObjC functions? ui.ImageContext doesn't have an objc counterpart. I'm specifically trying to use a CAShapeLayer.

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

        Couldn't you just use ui.Path and add the points and paths drawings as a subview to the ui.View? Hopefully that made sense.

        Check out the sketch.py example in the Examples folder for an example.

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

          ok, simple, stupid example.

          import ui,time
          from objc_util import *
          # create a view
          v=ui.View()
          v.bg_color='white'
          vv=ObjCInstance(v)
          
          # grab view's layer
          layer=vv.layer()
          
          # add a shapelayer
          CAShapeLayer=ObjCClass('CAShapeLayer')
          shlayer=CAShapeLayer.layer()
          shlayer.frame=layer.bounds()
          layer.addSublayer_(shlayer)
          
          # add some stuff to the shapelayer, using a ui.Path
          p=ui.Path.rounded_rect(5,5,80,80,22)
          shlayer.path=ObjCInstance(p).CGPath()
          
          #stroke it
          shlayer.strokeColor = UIColor.colorWithRed(0.75,green=0.0,blue=0.0,alpha=1.0).CGColor()
          shlayer.fillColor=UIColor.colorWithRed(1,green=1.0,blue=1.0,alpha=1.0).CGColor()
          shlayer.lineWidth = 9
          shlayer.strokeEnd=.0
          
          #sublayer must be added before view is presented... dunno why.
          v.present('sheet')
          
          # demo.  seems to have built in default animation
          time.sleep(1)
          on_main_thread(lambda: shlayer.setStrokeEnd_(1))()
          time.sleep(1)
          on_main_thread(lambda: shlayer.setStrokeEnd_(0))()
          time.sleep(1)
          for i in range(30):
          	on_main_thread(lambda: shlayer.setStrokeEnd_(i/29.))()
          	time.sleep(.1)
          
          
          
          1 Reply Last reply Reply Quote 0
          • Webmaster4o
            Webmaster4o last edited by

            Thanks @JonB I came up with a similar example a few hours before you: https://github.com/controversial/ui2/blob/master/ui2/view_classes/ProgressPathView.py

            I should really report back here sooner when I find a solution.

            I had found addSublayer_() in a StackOverflow answer and realized it was perfect ;)

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