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

后端 未结 7 1694
攒了一身酷
攒了一身酷 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:07

    You can add SKView as a subview within your UIKit hierarchy. A function like the following would work, allowing you to create a UIImageView with the effect as a subview, and then you can add this to your main view. Be sure to link against SpriteKit.

    UIImageView *NewEffectUIImageViewWithFrame(CGRect frame)
    {
        UIImageView *tempView = [[UIImageView alloc] initWithFrame:frame];
    
        SKView *skView = [[SKView alloc] initWithFrame:CGRectMake(0.0, 0.0, frame.size.width, frame.size.height)];
        [tempView addSubview:skView];
    
        SKScene *skScene = [SKScene sceneWithSize:skView.frame.size];
        skScene.scaleMode = SKSceneScaleModeAspectFill;
        skScene.backgroundColor = [UIColor clearColor];
    
        SKEmitterNode *emitter =  [NSKeyedUnarchiver unarchiveObjectWithFile:[[NSBundle mainBundle] pathForResource:@"SparkParticle" ofType:@"sks"]];
        emitter.position = CGPointMake(frame.size.width*0.5,0.0);
    
        [skScene addChild:emitter];
        [skView presentScene:skScene];
    
        return tempView;
    }
    

    In the end, if all you need is an emitter, it may be easier to create a CAEmitterLayer and add that as a sublayer to your UIView instead. Of course, that means you have to programmatically create the CAEmitterLayer and can't use the cool Xcode particle editor...

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-07 11:11

    Actually there is a way to add particles without SpriteKit - CoreAnimation's CAEmitterCells.

    This way you can add particles in your UIView easily. If you want to play around with the parameters and get the code easily, get this app (Particle X).

    It also supports SpriteKit so if you want to play around or design particles on the go and immediately get the code for it, this app is the solution.

    PS. If you haven't noticed it, I am the developer of the app - made it to use it myself when designing app and games. :)

    0 讨论(0)
  • 2020-12-07 11:11

    Putting this here for visibility reasons.

    The answers regarding the user of a .clear backgroundColor are correct, except that you must also set the allowsTransparency property on SKView to 'true'.

    skView.allowsTransparency = true
    skView.backgroundColor = .clear  // (not nil)
    scene.backgroundColor = .clear
    

    If you don't set allowsTransparency to true, and you layout your SKView over, say, a UIImageView, the composition engine will have a fit, and will send your GPU red-lining, even if only a single particle is drawn. (In the Simulator, the CPU will spike instead.)

    0 讨论(0)
  • 2020-12-07 11:19

    In modern Xcode:

    This is now very easy.

    1. In Xcode, click to create a new

    "SpriteKit Particle File"

    it will be a single .sks file.

    (NOTE: Do NOT choose "SceneKit Particle System File". Choose "SpriteKit Particle File".)

    Click once on the .sks file. Notice the many controls on the right.

    The particles will actually be moving, it is a living preview. Anything that can be done with particles, you can do it. It is like using particles in a game engine, except performance is 18 billion times better.

    2. Have any ordinary UIView, anywhere you want:

    @IBOutlet weak var teste: UIView! // totally ordinary UIView
    

    3. Just use the following code to link:

    The following slab of code will put your new particle system, inside, the ordinary UIView "teste":

    import SpriteKit ...
    
    
    let sk: SKView = SKView()
    sk.frame = teste.bounds
    sk.backgroundColor = .clear
    teste.addSubview(sk)
    
    let scene: SKScene = SKScene(size: teste.bounds.size)
    scene.scaleMode = .aspectFit
    scene.backgroundColor = .clear
    
    let en = SKEmitterNode(fileNamed: "SimpleSpark.sks")
    en?.position = sk.center
    
    scene.addChild(en!)
    sk.presentScene(scene)
    

    Add this to anything you want.

    If you want a sparkling button, add it to a button.

    If you want the whole screen to shower rainbows, add it to a full-screen view.

    It's that easy.


    Example of how to use the SpriteKit Particle File controls:

    Say you want a burst of sparks, which ends.

    Set the max to 50...

    Tip - if your effect "finishes" (ie, it is not a loop), it seems you can simply get rid of the SKScene when finished. Like this:

        ...
        scene.addChild(en!)
        sk.presentScene(scene)
        
        delay(1.5) { sk.removeFromSuperview() }
    

    That one line of code at the end seems to clean-up everything.


    BTW if you want fantastic ideas for particle systems, a great idea is click to the Unity "asset store", where various particle artists buy and sell particle systems. Their work will give you great ideas.

    Just click "particles" in the list on the right; watch the videos. (Innovative examples .)


    Note! Apple are going to make it so that you can very simply make a SKView in storyboard, and select the .sks scene. However ..

    ... it does not work yet! It's still broken as of the last edit to this post (2020). So you need the code fragment above.

    0 讨论(0)
  • 2020-12-07 11:19

    You cannot use particle effects within UIView directly.

    SKEmitterNode must be in a node tree defined with a node scene (SKScene). The scene node runs an animation loop that renders the contents of the node tree for display. UIView is static, won't work for it.

    However, you probably able to create a scene inside your UIView, but I've never tried to do that.

    0 讨论(0)
提交回复
热议问题