使用CAGradientLayer 簡單做一個掃描動畫

直接上代碼
  override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.white
        // Do any additional setup after loading the view.
        
        tempView = UIView(frame: CGRect(x: 100, y: 200, width: 200, height: 300));
        tempView?.backgroundColor = UIColor.black
        self.view.addSubview(tempView!)

        let animation: CAGradientLayer = CAGradientLayer()
        animation.frame = CGRect(x: 0, y: 0, width: 200, height: 300)
        animation.startPoint = CGPoint(x: 0, y: 0)
        animation.endPoint = CGPoint(x: 0, y: 1)
        let color = UIColor(red: 216.0/255.0, green: 114.0/225.0, blue: 213.0/255.0, alpha: 1.0)
        let color1 = UIColor(red: 61.0/255.0, green: 226.0/225.0, blue: 210.0/255.0, alpha: 1.0)
        animation.colors = [UIColor.clear.cgColor,color1.cgColor, color.cgColor]
        animation.locations = [0.0,0.1,0.2]
        tempView?.layer.addSublayer(animation)
        
        let gradientAnimation: CABasicAnimation = CABasicAnimation()
        gradientAnimation.keyPath = "locations"
        gradientAnimation.fromValue = [0.0,0.1,0.2]
        gradientAnimation.toValue = [0.5,0.9,1.0]
        gradientAnimation.duration = 3.0
        gradientAnimation.repeatCount = 9;
        animation.add(gradientAnimation, forKey: nil);
}
效果圖如下
469A2AE5-4F7E-4A25-B3C2-A3CE0ADE19C1.GIF
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容