美文网首页swift编程开发
Swift 简单过渡动画

Swift 简单过渡动画

作者: 小黑Swift | 来源:发表于2016-03-12 01:59 被阅读184次

过渡动画

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 简单过渡动画

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