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

后端 未结 7 1716
攒了一身酷
攒了一身酷 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...

提交回复
热议问题