所以我开发了一个使用 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];
}