美文网首页
swift label简单实现 走马灯

swift label简单实现 走马灯

作者: 使胖子_iOS小白 | 来源:发表于2017-11-01 19:45 被阅读0次

    import UIKit

    class CyclicScrollingADView : UIView {

    varfirstLabel :UILabel?

    varsecondLabel :UILabel?

    varcurrentIndex :Int=0

    varstate :Bool=true

    lazyvartimer :Timer= {

    lettimer =Timer.init(timeInterval:4, target:self, selector:#selector(CyclicScrollingADView.timeChange), userInfo:nil, repeats:true)

    RunLoop.current.add(timer, forMode:RunLoopMode.commonModes)

    returntimer;

    }()

    vardataSource : [NSAttributedString] = [] {

    didSet{

    firstLabel?.attributedText=dataSource[0]

    currentIndex=0

    self.timer.fire()

    }

    }

    overrideinit(frame:CGRect) {

    super.init(frame: frame)

    self.clipsToBounds=true

    firstLabel=UILabel.init(frame:CGRect.init(x:0, y:0, width: frame.size.width, height: frame.size.height))

    firstLabel?.textColor=UIColor.white

    firstLabel?.font=UIFont.boldSystemFont(ofSize:11)

    secondLabel=UILabel.init(frame:CGRect.init(x:0, y: frame.size.height, width: frame.size.width, height: frame.size.height))

    secondLabel?.textColor=UIColor.white

    secondLabel?.font=UIFont.boldSystemFont(ofSize:11)

    self.addSubview(firstLabel!)

    self.addSubview(secondLabel!)

    }

    @objcfunctimeChange(){

    currentIndex=currentIndex+1

    ifcurrentIndex>=dataSource.count{

    currentIndex=0

    }

    ifstate{

    //展示第二个

    self.secondLabel?.attributedText=self.dataSource[currentIndex]

    UIView.animate(withDuration:0.5, animations: {

    self.firstLabel?.frame=CGRect.init(x:0, y: -self.frame.size.height, width:self.frame.size.width, height:self.frame.size.height)

    self.secondLabel?.frame=CGRect.init(x:0, y:0, width:self.frame.size.width, height:self.frame.size.height)

    }) { (finish)in

    self.firstLabel?.frame=CGRect.init(x:0, y:self.frame.size.height, width:self.frame.size.width, height:self.frame.size.height)

    }

    }else{

    //展示第一个

    self.firstLabel?.attributedText=self.dataSource[currentIndex]

    UIView.animate(withDuration:0.5, animations: {

    self.secondLabel?.frame=CGRect.init(x:0, y: -self.frame.size.height, width:self.frame.size.width, height:self.frame.size.height)

    self.firstLabel?.frame=CGRect.init(x:0, y:0, width:self.frame.size.width, height:self.frame.size.height)

    }) { (finish)in

    self.secondLabel?.frame=CGRect.init(x:0, y:self.frame.size.height, width:self.frame.size.width, height:self.frame.size.height)

    }

    }

    state= !state

    }

    requiredinit?(coder aDecoder:NSCoder) {

    fatalError("init(coder:) has not been implemented")

    }

    }

    相关文章

      网友评论

          本文标题:swift label简单实现 走马灯

          本文链接:https://www.haomeiwen.com/subject/ubbuuxtx.html