文字动画

swift版


文字动画_第1张图片

实现代码

//  View

let newView = UIView()

//  背景色

newView.backgroundColor = UIColor.blackColor()

//  创建label

let myLabel = UILabel(frame: CGRectMake(0,0,300,50))

myLabel.text = "房子房子房子房子房子房子"

myLabel.textColor = UIColor.whiteColor()

//  创建CAGradientLayer

let gradientLaber = CAGradientLayer()

//  什么颜色不重要,但必须写

gradientLaber.colors = [

UIColor.clearColor().colorWithAlphaComponent(0.3).CGColor,

UIColor.yellowColor().CGColor,

UIColor.yellowColor().colorWithAlphaComponent(0.3).CGColor

]

gradientLaber.locations = [(0.0),(0.0),(0.1)]

//  设置效果方向Y同X不同水平放方向,X同Y不同垂直方向

gradientLaber.startPoint = CGPoint(x: 0, y: 0)

gradientLaber.endPoint = CGPoint(x: 1 , y: 0)

gradientLaber.frame = myLabel.frame

// 创建核心动画动画

let animation = CABasicAnimation(keyPath: "locations")

animation.fromValue = [0.0,0.0,0.2]

animation.toValue = [0.9,1.0,1.0]

animation.duration = 2.5

animation.removedOnCompletion = false

animation.fillMode = kCAFillModeForwards

animation.repeatCount = Float.infinity

gradientLaber.addAnimation(animation, forKey: nil)

//  我也不懂?

myLabel.layer.mask = gradientLaber

newView.addSubview(myLabel)

你可能感兴趣的:(文字动画)