Gradient

Tram Ho

With gradients you will be able to create more color and beauty.

A color gradient represented as an array of color stops, each having a parametric location value.

Apple Documentation.

In this tutorial, you will learn how to implement different gradients in SwiftUI:

  • How to use Angular Gradient.
  • How to use Linear Gradient.
  • How to use a Radial Gradient.

Prerequisites

To follow along with this tutorial, you’ll need some basic knowledge about:

  • Basic with Swift.
  • Xcode 11 and above

Angular Gradient

With the Angular Gradient, you set the center of the circle and the gradient will start from the parameters you set and fill the color clockwise.

Currently, the gradient starts to fill at 180 degrees and moves clockwise.

You can even adjust the start and end angles of the gradient. You will set the starting angle to 90 and the end angle to 180.

If the center of the eye aches a little, you can move it back.

Linear Gradient

Linear gradients are the most commonly used gradients and have starting and ending points.

Apple has done very well in the previous determination UnitPoint using .leading , .trailing , .top , .topLeading , etc. All you have to know is that it starts and ends somewhere.

From left to right:

Up to down:

From left to top right:

In case you need a custom direction, you can even define your own Unit Points.

Radial Gradient

A Radial Gradient behaves like a Linear Gradient, except for the starting and ending points. With a Radial Gradient you specify the starting and ending radius, then the gradient will fill the circle and move out gradually.

To make it clearer, you just need to set the radius higher.

If you don’t like drawing at the center. You can define the starting point. You use .topLeading and it will appear at the top left.

What does it mean here?

Look at the image below, the radius starts at the inner circle and ends at the outer circle. Between the inner and outer circle, that’s where the gradient begins.

Thank you for watching the article, hope it will be helpful to you.

Share the news now

Source : Viblo