Search code examples
swiftmacosuser-interfacecaemitterlayer

CAEmitterLayer doesn't work in MacOS with Swift


I just simply create a storyboard program, and change the main ViewController code to the code below which works well in others' example. but it appears nothing on the view? seeking help.

What I did:

  1. create an project with storyboard, and I got 'AppDelegate.swift' and 'ViewController.swift'

  2. paste 'Ball_green.png' to my project and see it under the file tree of my project.

  3. paste the code below in 'ViewController.swift'

import Cocoa

class ViewController: NSViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do view setup here.
        self.view.wantsLayer = true
        self.addLayer()
    }
    
    func addLayer(){
        let rootLayer = self.view.layer
        let snowEmitter = CAEmitterLayer()
        snowEmitter.drawsAsynchronously = true
        snowEmitter.name = "snowEmitter"
        snowEmitter.zPosition = 10.0
        snowEmitter.emitterPosition = CGPoint(x: 0, y: 0)
        snowEmitter.renderMode = CAEmitterLayerRenderMode.backToFront
        snowEmitter.emitterShape = CAEmitterLayerEmitterShape.circle
        snowEmitter.emitterZPosition = -43.00
        snowEmitter.emitterSize = CGSize(width: 160, height: 160)
        snowEmitter.velocity = 20.57
        snowEmitter.emitterMode = CAEmitterLayerEmitterMode.points
        snowEmitter.birthRate = 10
        let snowFlakesCell2 = CAEmitterCell()
        snowFlakesCell2.emissionRange = .pi
        snowFlakesCell2.lifetime = 10.0
        snowFlakesCell2.birthRate = 4.0
        snowFlakesCell2.velocity = 2.0
        snowFlakesCell2.velocityRange = 100.0
        snowFlakesCell2.yAcceleration = 300.0
        snowFlakesCell2.contents = NSImage(named: "Ball_green.png")!.cgImage
        snowFlakesCell2.magnificationFilter = convertFromCALayerContentsFilter(CALayerContentsFilter.nearest)
        snowFlakesCell2.scale = 0.72
        snowFlakesCell2.scaleRange = 0.14
        snowFlakesCell2.spin = 0.38
        snowFlakesCell2.spinRange = 0
        snowEmitter.emitterCells = [snowFlakesCell2]
        rootLayer!.addSublayer(snowEmitter)
    }
}

fileprivate func convertFromCALayerContentsFilter(_ input: CALayerContentsFilter) -> String {
    return input.rawValue
}

Solution

  • extension NSImage {
        var cgImage: CGImage {
            get {
                let imageData = self.tiffRepresentation
                let source = CGImageSourceCreateWithData(imageData as! CFData, nil)
                let maskRef = CGImageSourceCreateImageAtIndex(source!, 0, nil)
                return maskRef!
            }
        }
    }
    

    I've solved the problem after adding the code.