addSublayer
let whiteBG = CALayer()
whiteBG.frame = bounds
whiteBG.cornerRadius = bounds.size.height/2.0
whiteBG.borderColor = UIColor.white.cgColor
whiteBG.borderWidth = 1.0
layer.addSublayer(whiteBG)
另一种遍历方法
var colors = [CGColor]()
for hue in stride(from: 0, through: 360, by: 5) {
let color = UIColor(hue: CGFloat(hue)/360.0, saturation: 1.0, brightness: 1.0, alpha: 1).cgColor
colors.append(color)
}
CALayer
let maskLayer = CALayer()
maskLayer.cornerRadius = bounds.size.height/2.0
maskLayer.backgroundColor = UIColor.white.cgColor
gradientLayer.mask = maskLayer
自定义控件
ColorProgress.swift
CAGradientLayer添加animation
let animation = CABasicAnimation(keyPath: "colors")
animation.fromValue = gradientLayer.colors
animation.toValue = colors
animation.duration = 0.08
animation.fillMode = kCAFillModeForwards
animation.delegate = self
gradientLayer.add(animation, forKey: "gradient")
<li><h1>学习代码:<h1></li>
ColorProgress.swift
import UIKit
class ColorProgress: UIView, CAAnimationDelegate {
let gradientLayer = CAGradientLayer()
let maskLayer = CALayer()
var progress: CGFloat = 0.0 {
didSet {
changeMaskFrame()
}
}
override init(frame: CGRect) {
super.init(frame: frame)
setupView()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func setupView() {
let whiteBG = CALayer()
whiteBG.frame = bounds
whiteBG.cornerRadius = bounds.size.height/2.0
whiteBG.borderColor = UIColor.white.cgColor
whiteBG.borderWidth = 1.0
layer.addSublayer(whiteBG)
gradientLayer.frame = bounds
gradientLayer.cornerRadius = bounds.size.height/2.0
gradientLayer.borderColor = UIColor.white.cgColor
gradientLayer.borderWidth = 1.0
var colors = [CGColor]()
for hue in stride(from: 0, through: 360, by: 5) {
let color = UIColor(hue: CGFloat(hue)/360.0, saturation: 1.0, brightness: 1.0, alpha: 1).cgColor
colors.append(color)
}
gradientLayer.colors = colors
gradientLayer.startPoint = CGPoint(x: 0.0, y: 0.5)
gradientLayer.endPoint = CGPoint(x: 1.0, y: 0.5)
layer.addSublayer(gradientLayer)
//添加显示区域
changeMaskFrame()
maskLayer.cornerRadius = bounds.size.height/2.0
maskLayer.backgroundColor = UIColor.white.cgColor
gradientLayer.mask = maskLayer
performAnimation()
}
private func changeMaskFrame() {
maskLayer.frame = CGRect(x: 0.0, y: 0.0, width: progress*bounds.size.width, height: bounds.size.height)
}
private func performAnimation() {
var colors = gradientLayer.colors
let color = colors?.popLast() as! CGColor
colors?.insert(color, at: 0)
let animation = CABasicAnimation(keyPath: "colors")
animation.fromValue = gradientLayer.colors
animation.toValue = colors
animation.duration = 0.08
animation.fillMode = kCAFillModeForwards
animation.delegate = self
gradientLayer.add(animation, forKey: "gradient")
gradientLayer.colors = colors
}
//动画停止后继续动画
func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
performAnimation()
}
}
ViewController.swift
import UIKit
let YHRect = UIScreen.main.bounds
let YHHeight = YHRect.size.height
let YHWidth = YHRect.size.width
let ProgressRect = CGRect(x: 20, y: 200, width: YHWidth-40, height: 20)
class ViewController: UIViewController {
let colorProgress = ColorProgress(frame: ProgressRect)
override func viewDidLoad() {
super.viewDidLoad()
setupView()
Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { (time) in
self.colorProgress.progress += 0.08
if self.colorProgress.progress >= 1.0 {
time.invalidate()
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func setupView() {
view.backgroundColor = .black
view.addSubview(colorProgress)
}
}