怎么样,效果是不是很棒!
不多说,直接上代码。
//
// 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
网友评论