swift 跑马灯

最近公司项目需要写一个跑马灯的效果,android的直接设置属性就可以了。为啥ios没有呢,我想一定是乔帮主认为跑马灯是个反人类的设计。哈哈,随便扯个蛋,进入正题。实现原理就是使用2个label,一前一后2个label的layer进行平行移动,当后面label的layer移动到前面的位置,动画再复原,重复执行。这样一个基本的跑马灯就实现了,我在此基础上给跑马灯加了,开始,停止,暂停,继续,4个动作。

动画代码

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

开始

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

停止

    // MARK: - 停止跑马灯动画
    func stop() {
        if isRun {
            isRun = false
            frontLabel.layer.removeAnimationForKey("frontAnima")
            behindLabel.layer.removeAnimationForKey("behindAnima")
        }
    }

暂停

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

继续

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

总结

上面就是我实现跑马灯的核心代码。在网上找了一番都没有遇到满意的跑马灯代码,就自己写了一份,欢迎大家前来交流。如果是使用oc的,那就需要你自己翻译成oc代码了_ swift跑马灯代码

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

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,282评论 4 61
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,332评论 25 709
  • 大概在5、6年前,也就是新浪微博还没诞生的时候,我曾经坚持写过好几年的博客。 当年博客更新的频率大概是2、3天一篇...
    温暖的鱼阅读 388评论 1 6
  • 李熬 “我不能靠有灵感才能写作,要靠习惯,只靠灵感绝不能成为好作家。” 海明威 “唯一的要求就是写一句真实的句子。...
    十六画生的写作阅读 181评论 0 0
  • 第二次机器革命正在走来,在指数级增长、数字化进步和组合式创新的驱动下,财富会增长得很快,并形成一个赢家通吃的市场,...
    镇里人阅读 278评论 0 0