To create a gradient in Swift, you can use the CAGradientLayer
class that is part of the Core Animation framework. Here's an example of how to create a vertical gradient with blue and green colors:
main.swift279 chars7 lines
In this code, we first create a CAGradientLayer
object and set its frame to the bounds of the current view. We set the colors property to an array of two colors (blue and green) converted to CGColor
objects. We specify a vertical gradient by setting the startPoint
to the middle of the top edge of the layer and the endPoint
to the middle of the bottom edge of the layer. Finally, we add the gradientLayer
as a sublayer to the view's layer.
You can also create more complex gradients by adding additional colors to the colors
array or by changing the startPoint
and endPoint
properties to create different gradients.
gistlibby LogSnag