2

所以我开发了一个使用 CAEmitterLayer 的应用程序。最初我让它自动发射(当应用程序加载时,它会开始发射火花),它在模拟器和设备上都运行良好。

现在,我将其更改为在用户点击屏幕时发出。它在模拟器中运行良好。在设备上,它没有反应。请在正确的方向上帮助我。

初始化敲入-viewDidLoad

tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];

分配于-viewWillAppear

tap setNumberOfTapsRequired:1];
[self.view addGestureRecognizer:tap];

- (void) particles {

    if (!sparkling) {

        rootLayer = [CALayer layer];
        rootLayer.bounds = CGRectMake(0, 0, 320, 480);
        CGColorRef color = [[UIColor clearColor] CGColor];
        rootLayer.backgroundColor = color;
        CGColorRelease(color);

        const char* fileName = [[[NSBundle mainBundle] pathForResource:@"DazStarOutline" ofType:@"png"] UTF8String];
        CGDataProviderRef dataProvider = CGDataProviderCreateWithFilename(fileName);
        id img = (__bridge id)CGImageCreateWithPNGDataProvider(dataProvider, NULL, NO, kCGRenderingIntentDefault);

        const char* fileNameR = [[[NSBundle mainBundle] pathForResource:@"DazRing" ofType:@"png"] UTF8String];
        CGDataProviderRef dataProviderR = CGDataProviderCreateWithFilename(fileNameR);
        id imgR = (__bridge id)CGImageCreateWithPNGDataProvider(dataProviderR, NULL, NO, kCGRenderingIntentDefault);

        mortor = [CAEmitterLayer layer];
        mortor.emitterPosition = CGPointMake(320, 0);
        mortor.renderMode = kCAEmitterLayerAdditive;

        CAEmitterCell *rocket = [CAEmitterCell emitterCell];
        rocket.emissionLongitude = M_PI / 2;
        rocket.emissionLatitude = 0;
        rocket.lifetime = 2.0;
        rocket.birthRate = 1;
        rocket.velocity = 400;
        rocket.velocityRange = 100;
        rocket.yAcceleration = -250;
        rocket.emissionRange = M_PI / 4;
        color = [[UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:0.5] CGColor];
        rocket.color = color;
        CGColorRelease(color);
        rocket.redRange = 0.5;
        rocket.greenRange = 0.5;
        rocket.blueRange = 0.5;

        CAEmitterCell *spark = [CAEmitterCell emitterCell];
        spark.contents = img;
        spark.lifetime = 0.05;
        spark.yAcceleration = -250;
        spark.beginTime = 0.8;
        spark.scale = 0.4;
        spark.birthRate = 10;

        preSpark.emitterCells = [NSArray arrayWithObjects:spark, nil];
        rocket.emitterCells = [NSArray arrayWithObjects:flare, nil];
        mortor.emitterCells = [NSArray arrayWithObjects:rocket, nil];
        [rootLayer addSublayer:mortor];
        [self.view.layer addSublayer:rootLayer];

        sparkling = TRUE;
    }
}

点击手势方法

- (void)handleSingleTap:(UIGestureRecognizer *)gestureRecognizer {

    [self particles];
}
4

1 回答 1

1

所以我下载了你的项目。两个问题 - 一个是 CFRelease 不正确 - 您不拥有该颜色。但更重要的是,如果您从 UIColor CGColor 中获取颜色,您应该真正使用 CG 方法来复制颜色,然后管理该资源。我知道这会变得复杂,但是当你混合 CG 和 UI 时,你会变得复杂——你无法避免它。

我将您的项目转换为 ARC,然后还创建了强大的 ivars 来保存您想要使用的 UIColors(作为 CGColorRefs)。您不能创建一个诱人的 UIColor,向它询问它的 CGColorRef,然后期望当 UIColor 消失时该颜色仍然存在。

稍作修改的项目,它在模拟器和设备(iPhone 4)上运行良好。

[注意 - CAEmitter 的使用非常好 - 漂亮!!!]

于 2012-09-01T01:17:37.740 回答