How to add particle effects to an iOS App that is not a game using iOS 7 SpriteKit Particle?

后端 未结 7 1712
攒了一身酷
攒了一身酷 2020-12-07 10:48

I need to add a rain particle effect to my app, I have been having a tough time finding ways to actually execute this idea.

I tried following this CALayer approach

7条回答
  •  执念已碎
    2020-12-07 11:10

    Here's approach totally different approach to try. My buddy gave me this cool way to go. Using CAEmitterCell. All in code! Looks like you need a spark.png image.

    extension UIView {
        final public func ignite() {
            let emitter = CAEmitterLayer()
            emitter.frame = self.bounds
            emitter.renderMode = kCAEmitterLayerAdditive
            emitter.emitterPosition = self.center
            self.layer.addSublayer(emitter)
    
            let cell = CAEmitterCell()
            let bundle = Bundle.init(for: UIColor.self)
            let image = UIImage(named: "spark", in: bundle, compatibleWith: traitCollection)
    
            cell.contents = image?.cgImage
            cell.birthRate = 1500
            cell.lifetime = 5.0
            cell.color = UIColor(red: 1.0, green: 0.5, blue: 0.1, alpha: 1).cgColor
            cell.alphaSpeed = -0.4
            cell.velocity = 50
            cell.velocityRange = 250
            cell.emissionRange = CGFloat.pi * 2.0
    
            emitter.emitterCells = [cell]
        }
    }
    

    Enjoy.

提交回复
热议问题