由下至上滾動效果的Label

1、封裝label

import UIKit
class MyFlashLabel: UIView {
    var innerContainer: UIView
    //計(jì)算text長度并返回
    func evaluateContentWidth(text: String) -> CGFloat {
        var height: CGFloat = 0.0
        let options: NSStringDrawingOptions = NSStringDrawingOptions.UsesLineFragmentOrigin
        if text.characters.count > 0 {
            let attributedDic = [NSFontAttributeName : UIFont.systemFontOfSize(12.0)]
            let size: CGSize = text.boundingRectWithSize(CGSizeMake(self.bounds.size.width - 20,CGFloat(MAXFLOAT)), options: options, attributes:attributedDic, context: nil).size
            height = size.height
        }
        return height
    }
    
    init(frame: CGRect, text: String) {
        self.innerContainer = UIView()
        super.init(frame: frame)
        self.innerContainer = UIView(frame: self.bounds)
        self.innerContainer.backgroundColor = UIColor.clearColor()
        self.clipsToBounds = true
        self.backgroundColor = UIColor.yellowColor()
        self.addSubview(self.innerContainer)
        print(self.subviews.count)
        self.flashAnimation(text)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    // 動畫
    func flashAnimation(text: String) {
        self.innerContainer.layer.removeAnimationForKey("move")
        let height: CGFloat = self.evaluateContentWidth(text)
        let label1: UILabel = UILabel(frame: CGRectMake(10,0,self.bounds.size.width - 10,height))
        label1.text = text
        label1.numberOfLines = 0
        label1.textColor = UIColor.greenColor()
        label1.font = UIFont.systemFontOfSize(12.0)
        label1.backgroundColor = UIColor.clearColor()
        self.innerContainer.addSubview(label1)
        
        if height > self.bounds.size.height {
            let label2: UILabel = UILabel(frame: CGRectMake(10,height + 5,self.bounds.size.width - 10,height))
            label2.text = text
            label2.numberOfLines = 0
            label2.backgroundColor = UIColor.clearColor()
            label2.font = UIFont.systemFontOfSize(12.0)
            label2.textColor = UIColor.greenColor()
            self.innerContainer.addSubview(label2)
            
            let moveAnimation: CAKeyframeAnimation = CAKeyframeAnimation(keyPath: "position.y")
            moveAnimation.keyTimes = [0.0,1.0]
            moveAnimation.duration = Double(height)/5.0
            moveAnimation.values = [0,-height - 5]
            moveAnimation.repeatCount = MAXFLOAT
            moveAnimation.timingFunction = CAMediaTimingFunction(name: "linear")
            self.innerContainer.layer.addAnimation(moveAnimation, forKey: "move")
        }
        
        
    }
}

2、添加到指定控制器

let labelView: MyFlashLabel = MyFlashLabel(frame: CGRectMake(20, 100, 280, 100), text: "\n滾動吧滾動吧滾動吧滾動吧滾動吧滾動吧滾動吧滾動吧滾動吧滾動吧滾動吧滾動吧滾動吧滾動吧滾動吧滾動吧滾動吧滾動吧滾動吧\n滾動吧滾動吧滾動吧滾動吧滾動吧滾動吧滾動吧滾動吧\n滾動吧滾動吧滾動吧滾動吧滾動吧滾動吧滾動吧滾動吧滾動吧滾動吧滾動吧滾動吧滾動吧滾動吧滾動吧滾動吧滾動吧滾動吧滾動吧")
self.view.addSubview(labelView)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,697評論 19 139
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,351評論 25 708
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,692評論 4 61
  • (本文并沒有潑臟水,也是針對那些腦殘粉強(qiáng)力洗白唐七的人,并非針對吃瓜群眾) 三生終于大結(jié)局了啊,小說抄吧,電視劇還...
    白樺樹木閱讀 353評論 0 1
  • “為什么有的人寫了十幾篇文章就受到了邀請,但是我寫了五十幾篇還是沒有受到邀請呢?” “通常持續(xù)寫多少篇原創(chuàng)文才會收...
    運(yùn)營小編閱讀 364評論 0 1

友情鏈接更多精彩內(nèi)容