UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

CALayer

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:... Continue Reading >>

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.... Continue Reading >>

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.... Continue Reading >>

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).... Continue Reading >>

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:... Continue Reading >>

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.... Continue Reading >>

How to emit particles using CAEmitterLayer

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 should exist.... Continue Reading >>

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.... Continue Reading >>

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.... Continue Reading >>

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:... Continue Reading >>

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.... Continue Reading >>

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:... Continue Reading >>

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.... Continue Reading >>

About the Swift Knowledge Base

This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions, all written for Swift 5.4.

BUY OUR BOOKS
Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.