【问题标题】:CAEmitterLayer animation that leaves accurate trail along the path沿路径留下准确轨迹的 CAEmitterLayer 动画
【发布时间】:2013-11-26 19:51:29
【问题描述】:

我需要用 CAKeyframeAnimation 动画的 UIView 留下痕迹

Ball * ball = [[Ball alloc]init];
// customized the ball 

CGMutablePathRef path = CGPathCreateMutable();
// filling in the path from points.

CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
anim.path = path;
anim.rotationMode = kCAAnimationRotateAuto;
anim.repeatCount = HUGE_VALF;
anim.duration = 1.2;
anim.cumulative = YES;
anim.additive = YES;

[ball.layer addAnimation:anim forKey:@"fly"];

现在 Ball.m 将具有以下发射器层:

@implementation TennisBall{

    __weak CAEmitterLayer *emitterLayer;

}


+ (Class)layerClass
{
    return [CAEmitterLayer class];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupLayer];
    emitterLayer = [CAEmitterLayer layer];
    emitterLayer.frame = self.bounds;
    emitterLayer.emitterPosition = CGPointMake(160, 240);
    emitterLayer.emitterSize = CGSizeMake(10, 10);
    emitterLayer.renderMode = kCAEmitterLayerAdditive;

    CAEmitterCell *emitterCell = [CAEmitterCell emitterCell];
    emitterCell.birthRate = 1;
    emitterCell.lifetime = 10.0;
    emitterCell.lifetimeRange = 0.5;
    emitterCell.velocity = 20;
    emitterCell.velocityRange = 10;
    emitterCell.emissionRange = 0;
    emitterCell.scaleSpeed = 0.3;
    emitterCell.spin = 0;
    emitterCell.color = [[UIColor colorWithRed:0.8 green:0.4 blue:0.2 alpha:0.1]
                         CGColor];
    emitterCell.contents = (id)[[UIImage imageNamed:@"first.png"] CGImage];
    [emitterCell setName:@"fire"];

    emitterLayer.emitterCells = [NSArray arrayWithObject:emitterCell];

    [self.layer addSublayer:emitterLayer];


   }
 return self;
}

我只需要球留下一条重复球行进路径的轨迹。我该如何实现?

【问题讨论】:

  • 你找到解决方案了吗?

标签: ios ios7 cakeyframeanimation caemitterlayer caemittercell


【解决方案1】:

为了确保发射器的位置在球层上是正确的, 您需要确保emitterPosition 与其anchorPoint 相同。

例如,如果anchorPoint 为(0,0),则emitterPosition 为(0,0)。 如果anchorPoint 是(0.5,0.5),size 是(0,0,300,300) 那么emitterPosition 是(150,150)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-23
    • 1970-01-01
    • 1970-01-01
    • 2019-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多