动画IOS收藏

【iOS】UILabel的缩放动画效果

2016-02-24  本文已影响3140人  刘大帅

学习文章

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

下载源码

下载地址

上一篇 下一篇

猜你喜欢

热点阅读