直接上代码
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
网友评论