Last Updated: February 25, 2016
·
1.273K
· supremegravity

Gradient fills with RubyMotion

It's easy to add a gradient to any UIView.

gview = UIView.alloc.initWithFrame([[0,0],[100,100]])
gradient = CAGradientLayer.layer
gradient.frame = gview.bounds
gradient.colors = [UIColor.blackColor, UIColor.purpleColor]
gview.layer.addSublayer(gradient) 
  • Beware that without the .CGColor on the two colors your app will crash.

If you've got the sugarcube gem in your project, you still need the .CGColor suffix:

gradient.colors = [:black.uicolor.CGColor, :purple.uicolor.CGColor]

Using BubbleWrap to set the view's frame to App.window.frame gives a full screen gradient view.

gview.frame = App.window.frame
App.window.addSubview gview

Picture

1 Response
Add your response

You can have multiple gradient sublayers. e.g. calling gview.layer.addSublayer(gradient) twice will yield

gview.layer.sublayers
=> [#<CAGradientLayer:0xc8f8690>, #<CAGradientLayer:0x158625d0>]

To change an existing gradient you need to modify one of the sublayers--adding another CAGradientLayer won't overwrite the existing gradient layers.

over 1 year ago ·