我需要在我的应用程序中添加雨粒子效果,我一直很难找到真正实现这个 idea 的方法.

我试着遵循这个CALayer方法教程:Link,但考虑到Xcode 5中提供的新iOS 7 SpriteKit粒子emits 器,我不确定这是否是最好的方法.

我已经创建了.sks文件,它在我的层次 struct 中,但我仍然无法将其添加到我的故事板/项目中.

话虽如此,How exactly do I add a SpriteKit Particle (sks) to my view? I am not at all familiar with scenes, layering , etc in the SpriteKit framework as I am not a game developer. I need the most details and sample code possible so that I can figure this out please

更新:

谢谢

推荐答案

UIView中创建SKScene以添加SKEmitterNode粒子效果.

一种方法是:

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包含在任何时候和任何地方touch SKView时创建粒子效果的代码.

#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.在主视图控制器中,包括场景类:

#import "MyScene.h"

并将代码添加到viewDidLoad以初始化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];
}

然后你应该在你的主UIView中有一个工作SKScene.

Objective-c相关问答推荐

跨平台 iPhone/Android 代码共享

如何用零填充左侧的整数?

如何声明仅调试语句

UITableViewCell 突出显示时使标签的背景清晰

是否需要 ARC 中的 NSNotificationCenter removeObserver?

带有语法高亮的 UITextView

来自 NSString 的 NSJSONSerialization

UIDatePicker,根据今天的日期设置最大和最小日期

我可以以编程方式滚动到 UIPickerView 中所需的行吗?

检测到与加载的库不匹配的 UUID

如何与 UITableView 一起滚动标题?

NSUserDefaults 的限制是什么?

有没有一种简单的方法可以将 ISO8601 时间戳转换为格式化的 NSDate?

OSStatus 错误代码 -34018

将 HTTP 标头添加到 NSURLRequest

WKWebView 判断 JavaScript 返回值

NSHTTPCookieStorage 状态未在应用退出时保存.那里有任何明确的知识/文档吗?

AVAudioPlayer 不播放任何声音

在 iOS 6.0 的 Xcode 4.5 中我没有要求的日志(log)消息

iOS中的活动生命周期相当于什么?