美文网首页
Swift 滚动label

Swift 滚动label

作者: 山有木枝壮 | 来源:发表于2017-02-20 16:15 被阅读288次

<pre>
/// 滚动label
class USTradeScrollLabelView: USBaseView {
fileprivate let scrollLabel: UILabel = UILabel()
var scrollText: String = ""

fileprivate var rectMark1: CGRect = CGRect.zero
fileprivate var rectMark2: CGRect = CGRect.zero

fileprivate var arrLabel: [UILabel] = []
var timeInterval: TimeInterval = 0.0
var isStop: Bool = false

init(info: String, frame: CGRect) {
    super.init(frame: frame)

// self.scrollText = info
self.scrollText = info
self.setupUI()
}

required public init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

func setupUI() {

// super.setup()

    self.scrollText = "   \(self.scrollText)   "
    self.timeInterval = self.displayDurationForString(self.scrollText)
    self.clipsToBounds = true
    
    self.scrollLabel.us.customize { (view) in
        view.text = self.scrollText
        view.font = USFont.t05.font
        view.us.handleTheme {
            view.textColor = USColor.c301.color
        }
    }
    
    let sizeOfText = self.scrollLabel.sizeThatFits(CGSize.zero)
    self.rectMark1 = CGRect(x: 0, y: 0, width: sizeOfText.width, height: self.bounds.size.height)
    self.rectMark2 = CGRect(x: self.rectMark1.origin.x + self.rectMark1.size.width, y: 0, width: sizeOfText.width, height: self.bounds.size.height)
    
    self.scrollLabel.frame = self.rectMark1
    self.addSubview(self.scrollLabel)
    
    self.arrLabel.append(self.scrollLabel)
    
    let useReserve = sizeOfText.width > self.frame.size.width ? true: false
    
    if useReserve {
        let reserveTextLabel = UILabel(frame: self.rectMark2)
        reserveTextLabel.us.customize({ (view) in
            view.text = self.scrollText
            view.font = USFont.t05.font
            view.us.handleTheme {
                view.textColor = USColor.c301.color
            }
            self.addSubview(view)
            self.arrLabel.append(reserveTextLabel)
            // label滚动
            self.scrollLabelIfNeeded()
        })
        
    }
    
}

fileprivate func scrollLabelIfNeeded() {
    if !self.isStop {
        let label1 = self.arrLabel[0]
        let label2 = self.arrLabel[1]
        
        UIView.transition(with: self, duration: self.timeInterval, options: [.curveLinear], animations: {
            label1.frame = CGRect(x: -self.rectMark1.size.width, y: 0, width: self.rectMark1.size.width, height: self.rectMark1.size.height)
            label2.frame = CGRect(x: label1.frame.origin.x + label1.frame.size.width, y: 0, width: label1.frame.size.width, height: label1.frame.size.height)
            
        }, completion: { [weak self](finished) in
            guard let `self` = self else {
                return
            }
            label1.frame = self.rectMark2
            label2.frame = self.rectMark1
            
            self.arrLabel[0] = label2
            self.arrLabel[1] = label1
            
            self.scrollLabelIfNeeded()
        })
    }
}

fileprivate func displayDurationForString(_ string: String) -> TimeInterval {
    let inter: TimeInterval = Double(string.characters.count / 3)
    return inter
}

func start() {
    self.isStop = false
    let label1 = self.arrLabel[0]
    let label2 = self.arrLabel[1]
    label1.frame = self.rectMark2
    label2.frame = self.rectMark1
    
    self.arrLabel[0] = label2
    self.arrLabel[1] = label1
    
    self.scrollLabelIfNeeded()
}

func stop() {
    self.isStop = true
}

}

</pre>

相关文章

  • Swift 滚动label

    /// 滚动labelclass USTradeScrollLabelView: USBaseView {file...

  • 菜单封装

    这个菜单选择的代码 点击滚动 滑动滚动 //// HWPageMenuView.swift// swift项...

  • Swift - Button,Label

    Swift-Button的常用 关于Button的一些类型 Swift里Label的使用

  • 懒加载相关笔记

    swift: 模板: var label = UILabel() //do something here labe...

  • 一个Label实现文字上下滚动

    Label向上滚动 播放系统消息 NSArray*arr =@[@"测试一",@"测试二",@"测试三",@"测试...

  • 循环滚动展示 label

    循环滚动展示 label业务需要做了一个上下滚动展示的文字广告位,类似轮播图的效果 Github 上的 Demo ...

  • 滚动播放Label封装

    相信大家有时候会遇到在首页上部展示一行文字,但是文字又很长无法只使用一行来展示,于是大家就想到平时我们在生活中看到...

  • UILabel中字体滚动显示

    记录下UILabel中字体超出Label宽度时滚动显示 SHFontCycleLabel.h SHFontCycl...

  • Swift Dynamic Label

    大家好,我是Tony,距离上次发帖有好久了。其实最近也挺迷茫的,不知道该是写中文贴还是英文贴。希望有大家给出自己的...

  • iOS文字颜色渐变透明

    渐变透明这种效果的文字一般都在可滚动的Label上,这样的效果能使Label看起来更加美观,同时可以提醒用户,还有...

网友评论

      本文标题:Swift 滚动label

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