vipul thummar
vipul thummar

Reputation: 342

Add Gradient layer (swift 2.3)

How to set gradient left to right?

let uiview : UIView = UIView.init(frame: CGRect(x: 50, y: 100, width: 100, height: 10))
let gradient = CAGradientLayer()
gradient.frame = uiview.bounds
gradient.colors = [UIColor.whiteColor().CGColor, UIColor.lightGrayColor().CGColor]
gradient.startPoint = CGPoint(x: 0, y: 0.5)
gradient.endPoint = CGPoint(x: 1, y: 0.5)
uiview.layer.insertSublayer(gradient, atIndex: 0)
self.view.addSubview(uiview)

Upvotes: 1

Views: 78

Answers (1)

leanne
leanne

Reputation: 8729

Take a look at CAGradientLayer - Core Animation | Apple Developer Documentation

Try something like:

let radians: CGFloat = CGFloat.pi
let x: CGFloat = 0.0
let y: CGFloat = 0.0
let z: CGFloat = 1.0

gradient.transform = CATransform3DMakeRotation(radians, x, y, z)

Of course, you can play around with the values till you get the exact result you want.

Upvotes: 1

Related Questions