Swift-直播點贊效果

Swift-直播點贊效果.png

怎么樣,效果是不是很棒!
不多說,直接上代碼。

//
//  ViewController.swift
//  Swift-直播點贊效果
//
//  Created by ibokan on 16/7/21.
//  Copyright ? 2016年 張宇. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    
    let kBounds = UIScreen.mainScreen().bounds.size

    override func viewDidLoad() {
        super.viewDidLoad()
        
        /*創建一個label*/
        let label = UILabel(frame: CGRectMake(0,0,100,30))
        label.center = self.view.center
        label.text = "點擊屏幕"
        label.backgroundColor = UIColor.clearColor()
        self.view.addSubview(label)
    }
    
    /*點擊屏幕方法*/
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        self.praise()
    }
    
    /*創建圖片動畫*/
    func praise(){
        let imageView = UIImageView()
        imageView.frame = CGRectMake(kBounds.width-60, kBounds.height-49, 35, 35)
        imageView.image = UIImage(named: "heart")
        imageView.backgroundColor = UIColor.clearColor()
        imageView.clipsToBounds = true
        self.view.addSubview(imageView)
        
        let startX = round(Double(random() % 200))
        let scale = round(Double(random() % 2)) + 1.0
        let speed = 1 / round(Double(random() % 900)) + 0.6
        let imageName:Int = Int(round(Double(random() % 12)))
        
        UIView.beginAnimations(nil, context: nil)
        UIView.setAnimationDuration(7 * speed)
        print(imageName)
        imageView.image = UIImage(named: "resource.bundle/heart\(imageName)")
        imageView.frame = CGRectMake(kBounds.width - CGFloat(startX), -100, 35 * CGFloat(scale), 35 * CGFloat(scale))
        
        UIView.setAnimationDidStopSelector(Selector("onAnimationComplete:finished:context:"))
        UIView.setAnimationDelegate(self)
        UIView.commitAnimations()
    }
    
    func onAnimationComplete(animationID:String,finished:NSNumber,context:Void){
        
        var imageView = UIImageView()
        imageView = ((context) as! UIImageView)
        imageView.removeFromSuperview()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

代碼寫的不是很規范,望大家多多指教。
Demo地址http://pan.baidu.com/s/1boCo7sb

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容