画代码
// MARK: - 跑马灯动画实现过程
func marqueeAnimation() {
let frontAnima = CABasicAnimation()
frontAnima.keyPath = "position"
frontAnima.fromValue = NSValue(CGPoint: CGPoint(x: self.frontLabel.frame.width / 2 , y: self.frame.height / 2))
frontAnima.toValue = NSValue(CGPoint: CGPoint(x: -1 * self.frontLabel.frame.width / 2, y: self.frame.height / 2))
frontAnima.duration = self.time
frontAnima.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
frontAnima.repeatCount = MAXFLOAT
let behindAnima = CABasicAnimation()
behindAnima.keyPath = "position"
behindAnima.fromValue = NSValue(CGPoint: CGPoint(x: self.frontLabel.frame.width / 2 * 3 , y: self.frame.height / 2))
behindAnima.toValue = NSValue(CGPoint: CGPoint(x: self.frontLabel.frame.width / 2, y: self.frame.height / 2))
behindAnima.duration = self.time
behindAnima.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
behindAnima.repeatCount = MAXFLOAT
self.frontLabel.layer.addAnimation(frontAnima, forKey: "frontAnima")
self.behindLabel.layer.addAnimation(behindAnima, forKey: "behindAnima")
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
开始
// MARK: - 开始跑马灯动画
func start(){
if isRun == false {
isRun = true
isSuspend = false
frontLabel.layer.timeOffset = 0.0
behindLabel.layer.timeOffset = 0.0
frontLabel.layer.speed = 1.0
behindLabel.layer.speed = 1.0
frontLabel.layer.beginTime = 0.0
behindLabel.layer.beginTime = 0.0
marqueeAnimation()
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
停止
// MARK: - 停止跑马灯动画
func stop() {
if isRun {
isRun = false
frontLabel.layer.removeAnimationForKey("frontAnima")
behindLabel.layer.removeAnimationForKey("behindAnima")
}
}
1
2
3
4
5
6
7
8
暂停
// MARK: - 暂停动画
func suspend() {
if isRun && isSuspend == false {
isSuspend = true
let frontPauseTime = frontLabel.layer.convertTime(CACurrentMediaTime(), fromLayer: nil)
frontLabel.layer.speed = 0.0
frontLabel.layer.timeOffset = frontPauseTime
let behindPauseTime = behindLabel.layer.convertTime(CACurrentMediaTime(), fromLayer: nil)
behindLabel.layer.speed = 0.0
behindLabel.layer.timeOffset = behindPauseTime
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
继续
// MARK: - 继续动画
func continuation() {
if isRun && isSuspend {
isSuspend = false
let frontPauseTime = frontLabel.layer.timeOffset
frontLabel.layer.speed = 1.0
frontLabel.layer.timeOffset = 0.0
frontLabel.layer.beginTime = 0.0
let frontTimeSincePause = frontLabel.layer.convertTime(CACurrentMediaTime(), fromLayer: nil) - frontPauseTime
frontLabel.layer.beginTime = frontTimeSincePause
let behindPauseTime = behindLabel.layer.timeOffset
behindLabel.layer.speed = 1.0
behindLabel.layer.timeOffset = 0.0
behindLabel.layer.beginTime = 0.0
let behindTimeSincePause = behindLabel.layer.convertTime(CACurrentMediaTime(), fromLayer: nil) - behindPauseTime
behindLabel.layer.beginTime = behindTimeSincePause
}
}
---------------------
作者:kanxuexizhong
来源:CSDN
原文:https://blog.csdn.net/kanxuexizhong/article/details/85024255
版权声明:本文为博主原创文章,转载请附上博文链接!
---------------------
作者:weixin_44129414
来源:CSDN
原文:https://blog.csdn.net/weixin_44129414/article/details/85237015
版权声明:本文为博主原创文章,转载请附上博文链接!