11.

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)
    }

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,198评论 4 61
  • 我是空气的化身,害羞时四处掩埋。有时我会很安静,坐在角落里,看着人们各自忙碌着。静静地欣赏人间的美好与温情。 我是...
    左岸花开2020阅读 604评论 2 4
  • 文/柳夕 乡思他人,梦笔回,千里雪。 荡酒绝世一杯,念君人,不有他乡圆如故。
    邵风阅读 198评论 0 7
  • 卜貓的彩鉛色卡之二(建設中)
    羊生生和卜猫阅读 1,090评论 0 1
  • 梦回廊阁怨四起,犹鬓鲜白貌声衰。 黄梁独钓心悸往,应有佳人狐媚笑。
    笔尖的灵感阅读 128评论 0 0