そこで、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];
}