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

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

    Create a SKScene in your UIView to add a SKEmitterNode particle effect.

    One way of doing this:

    1.In storyboard (or programatically if you prefer) add a View object on top of the existing View and resize it to your needs.
    2.Change the class of the new view to SKView
    3.In your view controller .h file create a property for the SKView:

    @property IBOutlet SKView *skView;
    

    4.Link the SKView on your storyboard to the skView property.
    5.Create a new class, subclassing SKScene. MyScene.h will look like:

    #import <SpriteKit/SpriteKit.h>
    @interface MyScene : SKScene
    @end
    

    MyScene.m below contains code to create a particle effect whenever and wherever the SKView is touched.

    #import "MyScene.h"
    
    @implementation MyScene
    
    -(id)initWithSize:(CGSize)size {    
        if (self = [super initWithSize:size]) {
            /* Setup your scene here */
    
            self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];
            SKLabelNode *myLabel = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"];
            myLabel.text = @"Hello, World!";
            myLabel.fontSize = 30;
            myLabel.position = CGPointMake(CGRectGetMidX(self.frame),
                                       CGRectGetMidY(self.frame));
    
            [self addChild:myLabel];
        }
        return self;
    }
    
    //particle explosion - uses MyParticle.sks
    - (SKEmitterNode *) newExplosion: (float)posX : (float) posy
    {
        SKEmitterNode *emitter =  [NSKeyedUnarchiver unarchiveObjectWithFile:[[NSBundle mainBundle] pathForResource:@"MyParticle" ofType:@"sks"]];
        emitter.position = CGPointMake(posX,posy);
        emitter.name = @"explosion";
        emitter.targetNode = self.scene;
        emitter.numParticlesToEmit = 1000;
        emitter.zPosition=2.0;
        return emitter;
    }
    
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        /* Called when a touch begins */
    
        for (UITouch *touch in touches) {
            CGPoint location = [touch locationInNode:self];
            //add effect at touch location
            [self addChild:[self newExplosion:location.x : location.y]];
        }
    }
    
    -(void)update:(CFTimeInterval)currentTime {
        /* Called before each frame is rendered */
    }
    
    @end
    

    6.In your main view controller, include your scene class:

    #import "MyScene.h"
    

    and add code to viewDidLoad to initialise the SKView:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        // Configure the SKView
        SKView * skView = _skView;
        skView.showsFPS = YES;
        skView.showsNodeCount = YES;
    
        // Create and configure the scene.
        SKScene * scene = [MyScene sceneWithSize:skView.bounds.size];
        scene.scaleMode = SKSceneScaleModeAspectFill;
    
        // Present the scene.
        [skView presentScene:scene];
    }
    

    You should then have a working SKScene within your main UIView.

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