【iOS】UILabel的缩放动画效果

学习文章

UILabel的缩放动画效果

效果

ScaleLabel.gif

简单原理

创建一个 ScaleLabel 的 View , View 上有两个 UILabel , 一个 Label 负责文本呈现,另一个 Label 负责缩放颜色的呈现.利用仿射的缩放变换和颜色透明度的变化,在 UIView 的动画中生动的变现出来.

源码

ScaleLabel.swift

import UIKit

class ScaleLabel: UIView {

    var m_text            : String? {
        
        willSet {
        
            m_backLabel.text  = newValue
            m_colorLabel.text = newValue
        }
    }
    
    var m_font            : UIFont? {
    
        willSet {
        
            m_backLabel.font  = newValue
            m_colorLabel.font = newValue
        }
    }
    var m_startScale      : Float? {
    
        willSet {
            
            m_backLabel.transform  = CGAffineTransformMake(CGFloat(newValue!), 0, 0, CGFloat(newValue!), 0, 0)
            m_colorLabel.transform = CGAffineTransformMake(CGFloat(newValue!), 0, 0, CGFloat(newValue!), 0, 0)
        }
    }
    var m_endScale        : Float?
    var m_backLabelColor  : UIColor? {
    
        willSet {
            
            m_backLabel.textColor = newValue
        }
    }
    var m_colorLabelColor : UIColor? {
    
        willSet {
        
            m_colorLabel.textColor = newValue
        }
    }
    
    private
    var m_backLabel  : UILabel!
    
    private
    var m_colorLabel : UILabel!
    
    func startAnimation() {
    
        if(self.m_endScale == 0) {
        
            self.m_endScale = 2.0
        }
        
        [UIView .animateWithDuration(1, delay: 0, usingSpringWithDamping: 7, initialSpringVelocity: 4, options: .CurveEaseInOut, animations: { [weak self]() -> Void in
            
            if let strongSelf = self {
            
                strongSelf.m_backLabel.alpha     = 1.0
                strongSelf.m_backLabel.transform = CGAffineTransformIdentity
                
                strongSelf.m_colorLabel.alpha     = 1.0
                strongSelf.m_colorLabel.transform = CGAffineTransformIdentity
            }
            
            }, completion: { (Bool) -> Void in
                
                [UIView .animateWithDuration(2, delay: 0.5, usingSpringWithDamping: 7, initialSpringVelocity: 4, options: .CurveEaseInOut, animations: { () -> Void in
                    
                    self.m_colorLabel.alpha     = 0
                    self.m_colorLabel.transform = CGAffineTransformMake(CGFloat(self.m_endScale!), 0, 0, CGFloat(self.m_endScale!), 0, 0)
                    
                    }, completion: nil)]
        })]
    }
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        self.m_backLabel  = UILabel(frame: self.bounds)
        self.m_colorLabel = UILabel(frame: self.bounds)
        
        self.addSubview(self.m_backLabel)
        self.addSubview(self.m_colorLabel)
        
        self.m_backLabel.alpha  = 0
        self.m_colorLabel.alpha = 0
        
        self.m_backLabel.textAlignment  = .Center
        self.m_colorLabel.textAlignment = .Center
    }

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

}

ViewController.swift

import UIKit

class ViewController: UIViewController {
    
    override func viewDidLoad() {
       
        super.viewDidLoad()
        
        self.view.backgroundColor = UIColor.blackColor()
            
        let scaleLabel1 :ScaleLabel = ScaleLabel(frame: CGRect(x: 0, y: 80, width: 300, height: 50))
        
        scaleLabel1.m_text            = "刘大帅"
        scaleLabel1.m_startScale      = 0.3
        scaleLabel1.m_endScale        = 2
        scaleLabel1.m_backLabelColor  = UIColor.whiteColor()
        scaleLabel1.m_colorLabelColor = UIColor.cyanColor()
        scaleLabel1.m_font            = UIFont(name: "Avenir-Light", size: 20)
        
        self.view.addSubview(scaleLabel1)
        
        let scaleLabel2 :ScaleLabel = ScaleLabel(frame: CGRect(x: 0, y: 180, width: 300, height: 50))
        
        scaleLabel2.m_text            = "Frank Liu"
        scaleLabel2.m_startScale      = 0.3
        scaleLabel2.m_endScale        = 2
        scaleLabel2.m_backLabelColor  = UIColor.cyanColor()
        scaleLabel2.m_colorLabelColor = UIColor.purpleColor()
        scaleLabel2.m_font            = UIFont(name: "Avenir-Light", size: 20)
        
        self.view.addSubview(scaleLabel2)
        
        let scaleLabel3 :ScaleLabel = ScaleLabel(frame: CGRect(x: 0, y: 280, width: 300, height: 50))
        
        scaleLabel3.m_text            = "You Xian Ming"
        scaleLabel3.m_startScale      = 0.3
        scaleLabel3.m_endScale        = 2
        scaleLabel3.m_backLabelColor  = UIColor.purpleColor()
        scaleLabel3.m_colorLabelColor = UIColor.orangeColor()
        scaleLabel3.m_font            = UIFont(name: "Avenir-Light", size: 20)
        
        self.view.addSubview(scaleLabel3)
        
        // 执行动画
        GCDQueue.mainQueue().execute({ () -> Void in
            
            scaleLabel1.startAnimation()
            scaleLabel2.startAnimation()
            scaleLabel3.startAnimation()
            
            }, afterDelaySecs: 1)
        
    }
}

下载源码

下载地址

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

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,259评论 4 61
  • 铭记在心 郭相麟 每个人来到这个世界,带着先天的遗传基因,带着后天的不懈努力,连接到宇宙爱的能量,会在迷茫中...
    郭相麟阅读 365评论 0 0
  • 夕阳把图书馆的屋顶划开与影子的距离, 若即若离,朦朦胧胧。 我身着白色的衬衫搭配天蓝色的牛仔裤, 白色帆布鞋踏着空...
    一揽芳华synh阅读 251评论 0 1
  • 听闻朋友驱车远道而来看我,很是高兴,做好了一切接待准备。 下午却又告知一为了节省经费,决定随大款们一起回来包吃包住...
    荷竹君阅读 234评论 0 0
  • 孔翛阅读 207评论 0 0