CABasicAnimation与CALayer.affineTransform无法正常工作

时间:2015-03-19 04:28:33

标签: ios core-animation

我有以下代码指定为轨道和行星演示:

    _orbitLayer.bounds = CGRectMake(0, 0, radius*2, radius*2);
    _orbitLayer.position = center;
    _orbitLayer.cornerRadius = radius;
    _orbitLayer.borderColor = [UIColor redColor].CGColor;
    _orbitLayer.borderWidth = 1.5;
    if ([_orbitLayer superlayer]) {
        [_orbitLayer removeAllAnimations];
        [_orbitLayer removeFromSuperlayer];
    }
    [self.layer insertSublayer:_orbitLayer above:_stripeLayer];

    // planet circyle
    _dotCircyleLayer.bounds = CGRectMake(0, 0, (arcWidth - 10),(arcWidth - 10));
    CGPoint anchor_center = CGPointMake(_orbitLayer.bounds.size.width,
                                        _orbitLayer.bounds.size.height/2);
    _dotCircyleLayer.position = anchor_center;
    _dotCircyleLayer.cornerRadius = (arcWidth - 10)/2;
    _dotCircyleLayer.backgroundColor = anchorColor.CGColor;
    if ([_dotCircyleLayer superlayer])
        [_dotCircyleLayer removeFromSuperlayer];
    [_orbitLayer addSublayer:_dotCircyleLayer];
    _orbitLayer.affineTransform = CGAffineTransformMakeRotation(startAngle);

// animation
    CABasicAnimation *arcAnimation = [CABasicAnimation animationWithKeyPath:@"affineTransform"];
    arcAnimation.duration = 0.8;
    arcAnimation.fromValue = [NSValue valueWithCGAffineTransform:CGAffineTransformMakeRotation(startAngle)];
    arcAnimation.toValue = [NSValue valueWithCGAffineTransform:CGAffineTransformMakeRotation(endAngle)];
    arcAnimation.removedOnCompletion = NO;
    [_orbitLayer addAnimation:arcAnimation forKey:nil];

我可以通过_orbitLayer.affineTransform = CGAffineTransformMakeRotation(startAngle);旋转此轨道来模拟行星环绕 但我不能动画_orbitLayer.affineTransform。我的动画代码不起作用。怎么了?

1 个答案:

答案 0 :(得分:2)

有趣的是,使用

arcAnimation.toValue = [NSNumber numberWithFloat:endAngle];

CABasicAnimation *arcAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];

救了我的一天

_orbitLayer.affineTransform = CGAffineTransformMakeRotation(startAngle);
CABasicAnimation *arcAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
arcAnimation.duration = 0.8;
//    arcAnimation.fromValue = [NSValue valueWithCGAffineTransform:CGAffineTransformMakeRotation(startAngle)];
//    arcAnimation.toValue = [NSValue valueWithCGAffineTransform:CGAffineTransformMakeRotation(endAngle)];
    arcAnimation.fromValue = [NSNumber numberWithFloat:startAngle];
    arcAnimation.toValue = [NSNumber numberWithFloat:endAngle];
    arcAnimation.removedOnCompletion = NO;
    [_orbitLayer addAnimation:arcAnimation forKey:nil];