Layer in swift 3.0

Overview

Layer is visual attributes you can set background color,border & show etc.. Almost UI control have one layer CALayer by using that you can use to provide border , round corner etc..

There are many layers in iOS but we are looking for few important layers here.

Topic 1: CAGradientLayer

Generally, if we have UI with gradient effect for 2 colours, we use image in our project which increases our app bundle size bit, so we have gradient layer to do it easily.

Just few lines of code and it’s done, check below code:

Here First we will make instance of layer then set frame.

colours: It’s an array property to set multiple colours

locations: Locations property is used to define area of colour between 0 to 1 range.

startPoint,endPoint: We have to provide directions of gradient, start & end position Default value is 0.5,0.5

screen1

Topic 2: CAShapeLayer

CAShapeLayer is used to draw custom UI using BezierPath object.

Using UIBezierPath we draw custom shape like polygon, triangle etc..

Here we make rounded layer object.

screen2

UIBezierPath

Let us create triangle path & apply to view.
First we create custom view & override draw method.

Here we are using move & addLine function we draw the path.

  • we will also fill pattern image in layer
  • fill() – fill the color inside triangle
  • strock() – Draw border on triangle

screen3

Topic 3: ReplicateLayer

Using RelicateLayer , We can create number of copies of sub-layers, each copy potentially having geometric, temporal, and color transformations applied to it.

Let’s create simple square relicatelayer see below code.

Here we first create instance of delicatelayer then create CALayer with green color & provide frame.

instanceCount

This property indicates number of copies you want to create.

instanceTransform

This property indicates the translation of each layer related to position

instanceGreenOffset,instanceGreenOffset

This is float value to create color (R,G,B) object

screen4

mm

Chirag Daslaniya Sr. iOS Developer

Chirag Daslaniya is a passionate Mobile Application Developer, especially for iOS at Yudiz Solutions Pvt. Ltd. – a leading iPhone App Development company. He is zealous about making easy-to-use and user-friendly applications using simple yet striking interfaces.

Comments are closed.

Top