Swift-粉碎动画效果

作者: 无穷369 | 来源:发表于2016-07-22 10:05 被阅读271次
    Swift-粉碎动画效果.gif

    先上一张GIF图片

    。。。图片是不是有点儿大呢?

    不要在意这些细节啦,上教学。

    还是老套路,首先将我Demo中的Smash文件夹拖入你的工程中,然后就可以调用了。

    //
    //  ViewController.swift
    //  Swift-粉碎动画效果
    //
    //  Created by ibokan on 16/7/22.
    //  Copyright © 2016年 张宇. All rights reserved.
    //
    
    import UIKit
    
    class ViewController: UIViewController {
        
        @IBOutlet var img1: UIImageView!
        @IBOutlet var img2: UIImageView!
        @IBOutlet var img3: UIImageView!
        @IBOutlet var img4: UIImageView!
        @IBOutlet var img5: UIImageView!
    
        /*点我粉碎按钮*/
        @IBAction func button(sender: UIButton) {
            
            delay(0, task: {self.img1.boom()})
            delay(1, task: {self.img2.boom()})
            delay(2, task: {self.img3.boom()})
            delay(3, task: {self.img4.boom()})
            delay(4, task: {self.img5.boom()})
        }
        
        /*点我重置按钮*/
        @IBAction func button2(sender: UIButton) {
            img1.reset()
            img2.reset()
            img3.reset()
            img4.reset()
            img5.reset()
        }
        
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
        
        typealias Task = (cancel : Bool) -> ()
        
        func delay(time:NSTimeInterval, task:()->()) ->  Task? {
            
            func dispatch_later(block:()->()) {
                dispatch_after(
                    dispatch_time(
                        DISPATCH_TIME_NOW,
                        Int64(time * Double(NSEC_PER_SEC))),
                    dispatch_get_main_queue(),
                    block)
            }
            
            var closure: dispatch_block_t? = task
            var result: Task?
            
            let delayedClosure: Task = {
                cancel in
                if let internalClosure = closure {
                    if (cancel == false) {
                        dispatch_async(dispatch_get_main_queue(), internalClosure);
                    }
                }
                closure = nil
                result = nil
            }
            
            result = delayedClosure
            
            dispatch_later {
                if let delayedClosure = result {
                    delayedClosure(cancel: false)
                }
            }
            
            return result;
        }
        
        func cancel(task:Task?) {
            task?(cancel: true)
        }
    
    }
    

    是不是很简单!
    Demo地址https://pan.baidu.com/s/1c7NiH4

    相关文章

      网友评论

        本文标题:Swift-粉碎动画效果

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