Welcome! In this article you will learn about the key theme of the iOS operating system: which is called "layers." You probably already know about View, but you may not know that each view is based on what is called a layer. Layers are part of the Core Animation framework.
You can say, “What does it make sense? I have never used layers before, so this may not be so important. ”Whether you know it or not, your application uses layers intensively. In iOS, each view is confirmed by a layer, no matter what it is. Layers exist in such a way that iOS can easily get raster information about views, with a view to its further transfer to the GPU (Graphics Processing Device). Look at the image below for a visual representation of where Core Animation is in the hierarchy of the iOS structure.

Why Layers? A bit of theory.
On devices like smartphones, users expect greater speed in everything they do. It is very important to support a consistent frame rate, which users perceive as “smooth operation”. On iOS devices, this rate is 60 frames per second. In order to save the system movements at this speed, a layer of graphical functionality was created that works directly on the GPU (graphics processing device): OpenGL.
')
OpenGL provides the lowest level (and fastest) access to iOS graphics devices. Nevertheless, there is a trade-off: OpenGL is very primitive, and even the simplest tasks require writing a very large amount of code.
To solve this problem, Core Graphics was created, which provides a slightly higher level of functionality with less code. For ease of working with Core Graphics, Core Animation was created. It provides the CALayer class, and allows you to get some basic low-level access to graphics capabilities.
After Apple decided that the more advanced Core Animation functionality is not always necessary in regular applications, UIKit was created, which provides the highest level of access to graphics. The advantage of this scheme is that you can choose which level of access to the chart you need in your application and select the required functionality, which can help in preventing the writing of unnecessary code.
The disadvantage is that higher API levels offer less functionality. The moral of the story is: CALayer exists so that IOS can quickly and easily create raster information about the presentation hierarchy of your application, which will then be transferred to Core Graphics, and ultimately to OpenGL, and subsequently displayed on the screen devices through the GPU. Despite the fact that there is no need to use CALayer directly in most cases, the low-level API provides developers with some more flexible settings that we will consider in this article.
Access to CALayer
It is enough to talk about why there are layers. Let's get to work! As I mentioned above, each view relies on a layer that can be accessed through the layer property for UIView. Assuming you have a MyView object, you can access its layer as follows:
myView.layer
Well, what can we do with the layer as soon as we have access to it? You will be surprised to see a huge number of new features. In the rest of the article I will talk about some of them.
Demo project
First, download the
starter project and let's get started! The best way to learn to do something is to practice, so in this application we will add custom effects for layers. When you open the project, you will see that this is a relatively simple application. This is a blank white view with a black square View in the center. Let's tidy it up a little. Go to ViewController.swift and get started.

Creating rounded corners
You can use the cornerRadius property from CALayer to round the corners. Let's try to do it. Inside viewDidLoad (), add the following line:
box.layer.cornerRadius = 5
As expected, this line of code will add an angular radius of 5 to the field layer. It looks like this:

Not so bad right? Increasing the corner radius makes the edges more rounded, and decreasing the corner radius makes the edges less rounded. By default, all layers have a corner radius of 0.

Adding a shadow effect
Shadow helps to create a sense of the depth of our application, and is very useful when designing interfaces. Using the shadow effect, we can create a “smooth” presentation effect on the device screen. Let's take a look at how to create a shadow effect using CALayer. Paste the following code in the viewDidLoad method in the ViewController:
box.layer.shadowOffset = CGSizeMake(5, 5)
Line 1: Sets the layer shadow offset to (5, 5). Setting (5, 5) as the value of layer.shadowOffset means that the shadow of the layer should be shifted 5 units to the right and 5 units below box.layer.
Line 2: This line sets the opacity of the layer shadow to 0.7. This means that the shadow should not exceed the opacity of more than 70%.
Line 3: This line sets the radius of the layer's shadow to 5. The radius of the shadow is the blur radius to apply to the shadow created by box.layer. A higher radius makes the shadow more blurry, but less noticeable. A smaller radius makes the shadow more visible, and more focused. The shadow radius 0 leads to full blur. In other words, through the above operations, it is possible to accurately determine the shape and size of the layer.
Line 4: This line sets the color of the shadow layer to dark gray. Note that this property is of type CGColor, not UIColor. Converting these two types is very easy. You just write myUIColor.CGColor.
Let's take a look at what we did!

Use of boundaries
CALayer also makes it easy to apply borders. Let's add a border to the field.
box.layer.borderColor = UIColor.blueColor().CGColor
Line 1: This line sets the color of the field border to dark blue. This will set a dark blue color for any border of the displayed field.
Line 2: This line sets the width of the field border to 3. This means that the border drawn around the field will be 3 units thick.
Let's see how the border will look like.

Image display
In addition, you can assign an image as a layer, in this way the layer displays the image. We have a beautiful picture of a tree, in our project, thanks to this
site . Let's make our layer display an image. Paste the following code into viewDidLoad:
box.layer.contents = UIImage(named: "Tree.jpg")?.CGImage
Line 1: It creates a new UIImage with the file name tree.jpg and assigns it to the contents layer property.
Line 2: It sets the size of the contents of the layer to resize it, which means that the entire contents of the layer will be resized to fit the size of the layer.
Line 3: We change the masksToBounds to true, so any sublayers of a layer that go beyond it will be cut to these boundaries. If you do not understand what this means, you can set it to false to see the differences.
Here is the result.

Background color and opacity
We talked about adding CALayer special effects that are not possible through the use of UIKit, but we should also discuss how you can change most of the UIKit properties that affect UIView by using CALayer. As an example, you can change the background color and transparency of the view, like the following:
box.layer.backgroundColor = UIColor.blueColor().CGColor box.layer.opacity = 0.5
CALayer specifications
Adding a large number of custom effects to layers can affect its characteristics. Now we will talk about two properties of CALayer, which can help us significantly improve application performance.
First, let's talk about drawsAsynchronously. This CALayer property determines whether a processor (CPU) needs to display a layer in a background thread. If set to true, the layer will look exactly the same as usual, but it will take a CPU calculation to display this calculation in the background thread. You must set the value to true if you have a view that needs to be redrawn frequently, such as a map or a table.
Next, consider the shouldRasterize. This is a CALayer property that determines whether a layer should be rasterized. When this property is true, the layer is drawn once. Whenever it is animated, it will not be redrawn and raster information will be deleted. This property should be set to true if you have a view that you do not need to redraw frequently. Note that when setting shouldRasterize, the display of the layer on Retina devices may change. This is because the layers have a so-called rasterization scale, through which the layer is rasterized. To prevent this, set rasterizationScale to UIScreen.mainScreen (). Scale so that the layer is rasterized at the same scale as the screen is displayed.
It should be noted that at 99%, you should not use any of these properties yourself. Installing them manually may result in poor performance. Set only one of these 2 properties at your discretion, if you determine that the image or layer affects the application performance.
Let's sum up
Now you know what CALayer is! With some knowledge of low-level graphics, you can create some interesting effects. I hope, for beginners, the article will be useful.
For reference, you can download the
finished project . If you have any questions or requests, please leave me a comment below.