Forum Archive

CoreAnimation / CAGradientLayer

reticulated

Hello Pythonistas,

I need some help - how can I use objc_util to use the CAGradientLayer class.

I can't seem to figure out what Framework(s) I might need to load.

Using ObjcClass it can't seem to find CAGradientLayer.

Ideally I want to setup a function which can generate buttons and views with a gradient as a background.

Any help would be appreciated!

mikael

@reticulated, looks like it is in the QuartzCore framework.

NSBundle.bundle(Path="/System/Library/Frameworks/QuartzCore.framework").load()

Would be interesting to see your results.

cvp

@reticulated I don't think we need something more than objectivec, see here for something similar.
But I've tried the same process without success, until now

cvp

@reticulated @mikael

Eureka (my error, colors need to be CGColors...)

from objc_util import *
import ui

v = ui.View()
v.frame = (0,0,300,300)
ov = ObjCInstance(v)
layer=ov.layer()

CAGradientLayer = ObjCClass('CAGradientLayer')
grlayer = CAGradientLayer.layer()
grlayer.frame=layer.bounds()

grlayer.setColors_([ObjCClass('UIColor').blueColor().CGColor(),ObjCClass('UIColor').redColor().CGColor()])

layer.addSublayer_(grlayer)

v.present('sheet')
cvp

If you prefer to be able to set r,g,b colors:

grlayer.setColors_([ObjCClass('UIColor').colorWithRed_green_blue_alpha_(1,0,0,1.0).CGColor(),ObjCClass('UIColor').colorWithRed_green_blue_alpha_(0.3,0,0,1.0).CGColor()])
reticulated

Hey thank you everyone.

I must have had a typo when I initially posted this - but it seems to work now.

Notes: I used ui.parse_color('blue').CGColor()

This works with RGBA, Hex and Named Colors.

Ill post the code when done, I am using this to create custom button styles, along with some custom styles for "checkbox" functionality (circles, squares, stars, etc)

Thanks again for your help!