CALayer - free Swift example code
About 4 min
CALayer - free Swift example code êŽë š
CALayer - free Swift example code
Learn Swift coding for iOS with these free tutorials â learn Swift, iOS, and Xcode
Found 13 articles in the Swift Knowledge Base for this category.
How to add a border outline color to a UIView
All
UIView
subclasses have a built-in way to draw a border around them using their underlying CALayer
. For example, to draw a 10-point red border around a view, you'd use this:... How to change a viewâs anchor point without moving it
Every
UIView
has an anchor point, which is the point around which animations take place. Usually this is the center of the view â X:0.5 Y:0.5 â which means if you rotate a view it will spin around its center....How to create a marching ants effect using lineDashPhase
âMarching antsâ is the informal name used for animation of a selection: you see a dashed line around whatever you selected, and the dashes slowly move around the selection to show that itâs active....
How to create keyframe animations using CAKeyframeAnimation
Keyframe animations offer extraordinary power for developers because they let you set multiple values and have iOS animate between them over times you specify. There are three components: a key path (the property to animate), an array of values (the value you want to use for that property), and an array of key times (when that value should be used for the property)....
How to draw color gradients using CAGradientLayer
I love
CAGradientLayer
because it takes just four lines of code to use, and yet looks great because it quickly and accurately draws smooth color gradients use Core Graphics. Here's a basic example:... How to draw shapes using CAShapeLayer
There are lots of
CALayer
subclasses out there, but CAShapeLayer
is one of my favorites: it provides hardware-accelerated drawing of all sorts of 2D shapes, and includes extra functionality such as fill and stroke colors, line caps, patterns and more....How to emit particles using CAEmitterLayer should exist....
Believe it or not, iOS has a built-in particle system that works great in all UIKit apps and is immensely customizable. To get started you need to create a
CAEmitterLayer
object and tell it how to create particles: where it should create them, how big the emitter should be, and what types of particles How to make a shape draw itself using strokeEnd
iOS makes it easy to draw shapes using
CAShapeLayer
, but you also get the ability to adjust how shapes are drawn. By manipulating the strokeStart
and strokeEnd
properties you can make shapes draw themselves on the screen: you can present a half-drawn star, or a three-quarters drawn circle, for example....How to make a UIView fade out
All views naturally fill the space assigned to them, but using
CAGradientLayer
as a mask view you can force a view to fade out at its edges....How to make a UIView glow using shadowColor
All views can have shadows thanks to the
CALayer
behind them, but you can use that same property to create glow effects. For example, this creates a 128x128 view then gives it a 20-point yellow glow:... How to round only specific corners using maskedCorners
You can set the
cornerRadius
property of any UIView
to have its edges rounded, but by default that rounds all corners at the same time. If you want only some corners to be rounded, you should set the layerâs maskedCorners
property to be an array of the corners you want â itâs an option set, so you can set one or many depending on your needs....How to round the corners of a UIView
All
UIView
subclasses have the ability to round their corners thanks to their underlying CALayer
â that's the bit that handles the actual drawing of your views. To round the corners of a view, use this code:... What is CALayer?
All
UIView
subclasses have a layer
property, which is responsible for drawing their contents efficiently. These layers are powered by Core Animation, which handles all the drawing and animation that UIKit requests. The layer is separate from the view because drawing is separate from placement â you can move a UIView
around freely, change its alpha
, add or remove it all you want, and so on, but that doesnât affect the underlying way itâs drawn....