import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
oneEmitterLayer()
twoEmitterLayer()
threeEmitterLayer()
// Do any additional setup after loading the view, typically from a nib.
}
func threeEmitterLayer(){
let rect = CGRect(x: view.bounds.width-100, y: view.bounds.height-100, width: 30, height: 30)
let emitter = CAEmitterLayer()
emitter.frame = rect
view.layer.addSublayer(emitter)
emitter.emitterPosition = CGPointMake(rect.width/2, rect.height/2)//发射源的位置
emitter.emitterSize = rect.size//发射源的尺寸
emitter.renderMode = kCAEmitterLayerUnordered
let array = NSMutableArray()
for i in 0 ..< 10 {
let stepCell = CAEmitterCell()
stepCell.birthRate = 1
stepCell.lifetime = Float(arc4random_uniform(4)+1)
stepCell.lifetimeRange = 1.5//actual lifetime will be between (lifetime-1.5 ~ lifetime+1.5)
let image = UIImage(named:NSString(format: "good%d_30x30", i) as String)
stepCell.contents = image?.CGImage
stepCell.velocity = CGFloat(arc4random_uniform(100)+50)
stepCell.velocityRange = 50
stepCell.emissionLongitude = CGFloat(M_PI+M_PI_2)
stepCell.emissionRange = CGFloat(M_PI_2/4)
stepCell.scale = 0.3
array.addObject(stepCell)
}
emitter.emitterCells = array as NSArray as? [CAEmitterCell]
}
func twoEmitterLayer() {
let rect = CGRect(x: 0.0, y: view.bounds.height-200, width: 200, height: 200)
let emitter = CAEmitterLayer()
emitter.frame = rect
view.layer.addSublayer(emitter)
emitter.renderMode = kCAEmitterLayerAdditive//合并粒子重叠部分的亮度使得看上去更亮
emitter.emitterPosition = CGPointMake(rect.width/2, rect.height/2)
let cell = CAEmitterCell()
let image = scaleImageToWidth(30,image: UIImage(named: "xin")!)
cell.contents = image.CGImage
cell.birthRate = 150 //每秒产生150个粒子
cell.lifetime = 5.0
cell.color = UIColor(red: 1.0, green: 0.5, blue: 0.1, alpha: 1.0).CGColor
cell.alphaSpeed = -0.4 //粒子的透明度每过一秒就减少0.4
cell.velocity = 50
cell.velocityRange = 20 //初始速度值变化的范围 30 ~ 70
cell.emissionLongitude = CGFloat(-M_PI_2)//向上(x-y平面的发射方向)
cell.emissionRange = CGFloat(M_PI_2/3)//围绕发射方向的弧度数
cell.scale = 0.3
emitter.emitterCells = [cell]
}
func oneEmitterLayer() {
let rect = CGRect(x: 0.0, y: -70.0, width: view.bounds.width,
height: 50.0)
let emitter = CAEmitterLayer()
// emitter.backgroundColor=UIColor.redColor().CGColor
emitter.frame = rect
view.layer.addSublayer(emitter)
emitter.emitterShape = kCAEmitterLayerRectangle//发射源的形状
//kCAEmitterLayerPoint
//kCAEmitterLayerLine
//kCAEmitterLayerRectangle
//position.x = frame.origin.x + 0.5 * bounds.size.width;
emitter.emitterPosition = CGPointMake(rect.width/2, rect.height/2)//发射源的位置
emitter.emitterSize = rect.size//发射源的尺寸
let emitterCell = CAEmitterCell()
let image = scaleImageToWidth(30,image: UIImage(named: "xh")!)
print(image.size.width)
emitterCell.contents = image.CGImage
emitterCell.birthRate = 120 //每秒产生120个粒子
emitterCell.lifetime = 3 //粒子生命周期
emitterCell.lifetimeRange = 1.0//2~4秒钟
emitterCell.spinRange = CGFloat(M_PI)// 自动旋转
emitter.emitterCells = [emitterCell] //这里可以设置多种粒子 我们以一种为粒子
emitterCell.yAcceleration = 70.0 //粒子Y方向一个加速度分量
// emitterCell.xAcceleration = 20.0 //粒子x方向一个加速度分量
emitterCell.velocity = 20.0 //初始速度
emitterCell.velocityRange = 20.0 //初始速度值变化的范围 0 ~ 40
emitterCell.emissionLongitude = CGFloat(M_PI_2) //向下(x-y平面的发射方向)
emitterCell.emissionRange = CGFloat(M_PI_2) 围绕发射方向的弧度数
emitterCell.scale = 0.8 //粒子的缩放比例
emitterCell.scaleRange = 0.8 //0 - 1.6
emitterCell.scaleSpeed = -0.15 //逐渐变小
emitterCell.alphaSpeed = -0.15 //透明度改变速度
emitterCell.alphaRange = 0.75 //一个粒子的颜色alpha能改变的范围
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/// 将当前图片缩放到指定宽度
///
/// - parameter width: 指定宽度
///
/// - returns: UIImage,如果本身比指定的宽度小,直接返回
func scaleImageToWidth(width: CGFloat,image:UIImage) -> UIImage {
// 1. 判断宽度,如果小于指定宽度直接返回当前图像
if image.size.width < width {
return image
}
// 2. 计算等比例缩放的高度
let height = width * image.size.height / image.size.width
// 3. 图像的上下文
let s = CGSize(width: width, height: height)
// 提示:一旦开启上下文,所有的绘图都在当前上下文中
UIGraphicsBeginImageContext(s)
// 在制定区域中缩放绘制完整图像
image.drawInRect(CGRect(origin: CGPointZero, size: s))
// 4. 获取绘制结果
let result = UIGraphicsGetImageFromCurrentImageContext()
// 5. 关闭上下文
UIGraphicsEndImageContext()
// 6. 返回结果
return result
}
}