import UIKit class ViewController: UIViewController{ override func viewDidLoad() { super.viewDidLoad() let view = UIView(frame: CGRect(x: 150, y: 250, width: 100, height: 100)) view.backgroundColor = UIColor.blue //CALayer邊框 view.layer.borderWidth = 2 view.layer.borderColor = UIColor.black.cgColor //CALayer陰影 view.layer.shadowColor = UIColor.gray.cgColor view.layer.shadowOffset = CGSize(width: 10, height: 10) view.layer.shadowOpacity = 0.5 view.layer.shadowRadius = 5.0 //CGLayer圓角 view.layer.masksToBounds = true//設置圓角半徑對view有用,但對view的內容不起做用,因此對內容進行裁邊 view.layer.cornerRadius = 100 self.view.addSubview(view) //CALayer漸變 let view2 = UIView(frame: CGRect(x: 150, y: 400, width: 100, height: 100)) let gradientLayer = CAGradientLayer() gradientLayer.frame = view2.bounds let formColor = UIColor.yellow.cgColor let midColor = UIColor.blue.cgColor let toColor = UIColor.red.cgColor gradientLayer.colors = [formColor,midColor,toColor] view2.layer.addSublayer(gradientLayer) //設置漸變方向 gradientLayer.startPoint = CGPoint(x: 0, y: 0)//值在0~1 gradientLayer.endPoint = CGPoint(x: 1, y: 1) //設置漸變行走軌跡 gradientLayer.locations = [0,0.5,1] self.view.addSubview(view2) } }