Swift CAEmitterCell CAEmitterLayer Sample Code

Swift CAEmitterCell Sample Code

Created By: Debasis Das (May 2016)

This post is inspired by the Objective C version of the Fire Demo Application from developer.apple.com and the flow is rewritten in Swift.

https://developer.apple.com/library/mac/samplecode/Fire/Introduction/Intro.html

The output of the program is as follows

Code

//  AppDelegate.swift
//  Swift_CAEmitterCell_Fire
//  Original Objective C Code https://developer.apple.com/library/mac/samplecode/Fire/Introduction/Intro.html
//  Created by Debasis Das on 28/05/16.
//  Copyright © 2016 Knowstack. All rights reserved.

import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    @IBOutlet weak var window: NSWindow!
    @IBOutlet weak var gasSlider:NSSlider!
    var rootLayer:CALayer = CALayer()
    var fireEmitterLayer:CAEmitterLayer = CAEmitterLayer()
    var smokeEmitterLayer:CAEmitterLayer = CAEmitterLayer()
    @IBOutlet weak var mainView:NSView!

    
    


    func applicationDidFinishLaunching(aNotification: NSNotification) {
        // Insert code here to initialize your application
    }

    func applicationWillTerminate(aNotification: NSNotification) {
        // Insert code here to tear down your application
    }

    override func awakeFromNib() {
        self.rootLayer.bounds = self.mainView.bounds
        let color = CGColorCreateGenericRGB(0.0, 0.0, 0.0, 1.0)
        rootLayer.backgroundColor = color
        
        let fireImage = NSImage(named: "fire")
        let fireImg:CGImageRef = (fireImage?.CGImageForProposedRect(nil, context: nil, hints: nil))!
        
        let smokeImage = NSImage(named: "fire")
        let smokeImg:CGImageRef = (smokeImage?.CGImageForProposedRect(nil, context: nil, hints: nil))!
        
        
        //Create the fire emitter layer
        self.fireEmitterLayer = CAEmitterLayer()
        self.fireEmitterLayer.emitterPosition = CGPointMake(225, 50)
        self.fireEmitterLayer.emitterMode = kCAEmitterLayerOutline
        self.fireEmitterLayer.emitterShape = kCAEmitterLayerLine
        self.fireEmitterLayer.renderMode = kCAEmitterLayerAdditive
        self.fireEmitterLayer.emitterSize = CGSizeMake(0, 0)
        
        //Create the smoke emitter layer
        self.smokeEmitterLayer = CAEmitterLayer()
        self.smokeEmitterLayer.emitterPosition = CGPointMake(225, 50)
        self.smokeEmitterLayer.emitterMode = kCAEmitterLayerPoints
        
        //Create the fire emitter cell
        let fireCell = CAEmitterCell()
        fireCell.emissionLongitude = CGFloat(M_PI);
        fireCell.birthRate = 0;
        fireCell.velocity = 80;
        fireCell.velocityRange = 30;
        fireCell.emissionRange = 1.1;
        fireCell.yAcceleration = 200;
        fireCell.scaleSpeed = 0.3;
        let fireColor = CGColorCreateGenericRGB(0.8, 0.4, 0.2, 0.10)
        fireCell.color = fireColor;
        fireCell.contents = fireImg
        fireCell.name = "fire" //Name the cell so that it can be animated later using keypaths
        
        
        //Add the fire emitter cell to the fire emitter layer
        self.fireEmitterLayer.emitterCells = [fireCell]
        
        //Create the smoke emitter cell
        let smokeCell = CAEmitterCell()
        smokeCell.birthRate = 11
        smokeCell.emissionLongitude = CGFloat(M_PI / 2)
        smokeCell.lifetime = 0
        smokeCell.velocity = 40
        smokeCell.velocityRange = 20
        smokeCell.emissionRange = CGFloat(M_PI / 4)
        smokeCell.spin = 1
        smokeCell.spinRange = 6
        smokeCell.yAcceleration = 160
        smokeCell.contents = smokeImg
        let smokeColor = CGColorCreateGenericRGB(0.2, 0.2, 0.2, 0.10)
        smokeCell.color = smokeColor;
        smokeCell.scale = 0.1
        smokeCell.alphaSpeed = -0.12
        smokeCell.scaleSpeed = 0.7
        smokeCell.name = "smoke" //Name the cell so that it can be animated later using keypaths
        
        //Add the smoke emitter cell to the smoke emitter layer
        self.smokeEmitterLayer.emitterCells = [smokeCell]
        
        //Add the two emitter layers to the root layer
        rootLayer.addSublayer(self.smokeEmitterLayer)
        rootLayer.addSublayer(self.fireEmitterLayer)
        
        
        self.mainView.layer = rootLayer
        self.mainView.wantsLayer = true
        self.mainView.needsDisplay = true
        
        self.sliderChanged(self)

    }
    
    @IBAction func sliderChanged(sender:AnyObject){
        let gas:Float = Float((self.gasSlider.intValue)/100)
        let birthRt:NSNumber = gas * 1000
        let gasRange:NSNumber = gas * 0.35
        let gasNum:NSNumber = gas
        self.fireEmitterLayer.setValue(birthRt, forKeyPath: "emitterCells.fire.birthRate")
        self.fireEmitterLayer.setValue(gasNum, forKeyPath: "emitterCells.fire.lifetime")
        self.fireEmitterLayer.setValue(gasRange, forKeyPath: "emitterCells.fire.lifetimeRange")
        self.fireEmitterLayer.emitterSize = CGSizeMake(CGFloat(50 * gas), 0)
        
        let smokeNum:NSNumber = gas * 4
        self.smokeEmitterLayer.setValue(smokeNum, forKeyPath: "emitterCells.smoke.lifetime")
        let color = CGColorCreateGenericRGB(0.2, 0.2, 0.2, CGFloat(gas * 0.1))
        self.smokeEmitterLayer.setValue(color, forKeyPath: "emitterCells.smoke.color")

    }
}


Swift CAEmitterCell CAEmitterLayer 3

Swift CAEmitterCell CAEmitterLayer 3

Swift CAEmitterCell CAEmitterLayer 2

Swift CAEmitterCell CAEmitterLayer 2

Swift CAEmitterCell CAEmitterLayer 1

Swift CAEmitterCell CAEmitterLayer 1

The Code can be downloaded here Swift_CAEmitterCell_Fire

Posted in Swift Tagged with: , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Recent Posts


Hit Counter provided by technology news