Swift 简单过渡动画

过渡动画

import UIKit

let SCREEN_HEIGHT = UIScreen.mainScreen().bounds.size.height
let SCREEN_WDITH = UIScreen.mainScreen().bounds.size.width

class ViewController: UIViewController {
    
    var splashView: UIView!
    var iconImgV: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 设置背景
        let photoImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: SCREEN_WDITH , height: SCREEN_HEIGHT))
        photoImageView.image = UIImage(named: "timg")
        photoImageView.center = self.view.center
        self.view.addSubview(photoImageView)
        
        // 设置View
        splashView = UIView(frame: CGRect(x: 0, y: 0, width: SCREEN_WDITH, height: SCREEN_HEIGHT))
        splashView.backgroundColor = UIColor(red: 64/255, green: 153/255, blue: 255/255, alpha: 1)
        self.view.addSubview(splashView)
        
        // 设置图标
        iconImgV = UIImageView(image: UIImage(named: "soup"))
        iconImgV.image?.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
        iconImgV.frame = CGRect(x: 0, y: 0, width: 120, height: 120)
        iconImgV.tintColor = UIColor.whiteColor()
        iconImgV.contentMode = UIViewContentMode.ScaleAspectFill
        iconImgV.center = splashView.center
        splashView.addSubview(iconImgV)
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        /*动画之前等待一秒*/
        after_delay(1) { () -> Void in
            self.splashAnimation()
        }
    }
    
    internal func after_delay(time:NSTimeInterval,block: dispatch_block_t){
        let time_uint64:UInt64 = UInt64(time)
        let delay = dispatch_time(DISPATCH_TIME_NOW, Int64(time_uint64 * NSEC_PER_SEC))
        dispatch_after(delay, dispatch_get_main_queue(), block)
    }

    private func splashAnimation() {
        
        // 持续时间
        let shrinkDuration = 1.5
        let growDuration = 0.7
        
        weak var weakSelf = self
        
        // 弹簧效果
        UIView.animateWithDuration(shrinkDuration, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 7, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
            
            let scaleTransform = CGAffineTransformMakeScale(0.75, 0.75)
            weakSelf!.iconImgV.transform = scaleTransform
            
            }) { (finished) -> Void in
                
                // 放大消失
                UIView.animateWithDuration(growDuration, animations: { () -> Void in
                    
                    let scaleTransForm2 = CGAffineTransformMakeScale(20, 20)
                    weakSelf!.iconImgV.transform = scaleTransForm2
                    
                    }, completion: { (finished) -> Void in
                // 移除View
                        weakSelf!.splashView.removeFromSuperview()
                })
        }
        
    }

}

你可能感兴趣的:(Swift 简单过渡动画)