【发布时间】:2015-02-07 18:17:16
【问题描述】:
我正在使用 CoreAnimation 创建一个非常简单的粒子系统。
这是我的牢房:
UIImage *image = [UIImage imageNamed:@"spark"];
CAEmitterCell *cell = [CAEmitterCell emitterCell];
[cell setContents:(id)image.CGImage];
[cell setBirthRate:250.f];
[cell setScale:.25f];
[cell setColor:[UIColor colorWithRed:1.0 green:0.2 blue:0.1 alpha:0.5].CGColor];
[cell setLifetime:5.0f];
和层:
CAEmitterLayer *emitterLayer = [CAEmitterLayer layer];
[emitterLayer setEmitterCells:@[cell]];
[emitterLayer setFrame:bounds];
[emitterLayer setRenderMode:kCAEmitterLayerAdditive];
[self.view.layer addSublayer:emitterLayer];
现在,我将发射器层的位置移动到我要触摸的任何位置:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
self.emitterLayer.emitterPosition = [[touches anyObject] locationInView:self.view];
}
但是,问题是,它不会连续发射粒子。而是每隔一定的时间(点与线相反):
我想也许是因为我没有为它制作动画。所以我尝试添加一个简单的动画来将发射器从屏幕的左上角移动到右下角,看看是否可行:
CGPoint startPos = CGPointZero;
CGPoint endPos = CGPointMake(bounds.size.width, bounds.size.height);
CABasicAnimation* ba = [CABasicAnimation animationWithKeyPath:@"emitterPosition"];
ba.fromValue = [NSValue valueWithCGPoint:startPos];
ba.toValue = [NSValue valueWithCGPoint:endPos];
ba.duration = .5f;
[emitterLayer addAnimation:ba forKey:nil];
我预计会形成一条由连续发射的粒子形成的线。但是,我看到的粒子是间隔发射的,像这样:
是否可以让emitterLayer 跟随您的修饰并像绘图一样连续发射这些粒子而不是点缀?
谢谢
【问题讨论】:
标签: ios objective-c iphone core-animation particle-system