Gradient Views w/Swift
When you want to give the background color to the view, but you want to add more colors and get the gradient effect, you need to add a layer to view,
which we are going to create, in few lines of code.
- Create a CAGradientLayer
var gradientLayer: CAGradientLayer = {
let gradientLayer =CAGradientLayer()
gradientLayer.colors = [#Color1, #Color2] //note: colors you want to use
gradientLayer.startPoint = CGPoint(x:0 y:0)
gradientLayer.endPoint = CGPoint(x:1 y:1)
gradientLayer.frame= CGRect.zero
return gradientLayer
} ()
- Add gradientLayer to the view layer
currentView.layer.addSublayer(gradientLayer)
- Set frame of the gradientLayer
gradientLayer.frame = currentView.bounds
- Published in blog, iOS, Programming Languages, Swift
Working w/Environments in Swift
According to Wikipedia, an Environment Variable is a dynamic-named value that can affect the way running processes will behave on a computer. They are part of the environment in which a process runs. Basically, many developers create 3 environments
- Dev
- Preprod
- Prod
Sometimes we need to separate the base URL and do the initial work which should be done for the following state or condition only.
Create enum for environments
if you need a refresher on enum check out the following article
enum Environment {
case dev
case preprod
case prod
}
Environment Variable
let currentEnvironment = Environment.dev
Execute using a switch statement
switch currentEnvironment {
case .dev:
print("execute only if the environment is development")
case .preprod:
print("execute only if the environment is preprod")
case .prod:
print("execute only if the environment is prod")
default:
break
}
Execute code now
First, we initialize the currentEnvironment variable and using conditions use it. Now we can change the baseUrl in AppDelegate along with other interesting code.
- Published in blog, iOS, Programming Languages, Swift




