3

この前のCAShapeLayerスレッドの回答を使用してで点滅するストロークをアニメーション化することに失敗しました。多くの検索の後、 を使用してストロークをアニメーション化する他の例を見つけることができません。CABasicAnimation

私がやりたいのは、CAShapeLayer2 つの色の間で脈拍を調整することです。CABasicAnimationopacity の使用は問題なく機能しますが、[CABasicAnimation animationWithKeyPath:@"strokeColor"]私にはわかりません。実装を成功させる方法についてアドバイスをいただければ幸いです。

    CABasicAnimation *strokeAnim = [CABasicAnimation animationWithKeyPath:@"strokeColor"];
    strokeAnim.fromValue         = (id) [UIColor blackColor].CGColor;
    strokeAnim.toValue           = (id) [UIColor purpleColor].CGColor;
    strokeAnim.duration          = 1.0;
    strokeAnim.repeatCount       = 0.0;
    strokeAnim.autoreverses      = NO;
    [shapeLayer addAnimation:strokeAnim forKey:@"animateStrokeColor"];

//    CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
//    opacityAnimation.fromValue         = [NSNumber numberWithFloat:0.0];
//    opacityAnimation.toValue           = [NSNumber numberWithFloat:1.0];
//    opacityAnimation.duration          = 1.0;
//    opacityAnimation.repeatCount       = 0.0;
//    opacityAnimation.autoreverses      = NO;
//    [shapeLayer addAnimation:opacityAnimation forKey:@"animateOpacity"];

不透明度アニメーションのコメントを外すと、予期される不透明度のフェードが発生します。ストローク アニメーションは効果がありません。暗黙的な strokeColor の変更は期待どおりにアニメーション化しますが、CABasicAnimation を使用して strokeColor を明示的にアニメーション化できることを文書化して確認したいと思います。

更新: 具体的な問題は、shapeLayer.path が NULL だったことです。それを修正すると、問題が修正されました。

4

1 に答える 1

7

以下のコードは私にとってうまく機能します。shapeLayer ストローク パスの lineWidth はいくつですか? それが問題でしょうか?

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIBezierPath * circle = [UIBezierPath bezierPathWithOvalInRect:self.view.bounds];

    CAShapeLayer * shapeLayer = [CAShapeLayer layer];
    shapeLayer.path = circle.CGPath;
    shapeLayer.strokeColor =[UIColor blueColor].CGColor;
    [shapeLayer setLineWidth:15.0];

    [self.view.layer addSublayer:shapeLayer];

    CABasicAnimation *strokeAnim = [CABasicAnimation animationWithKeyPath:@"strokeColor"];
    strokeAnim.fromValue         = (id) [UIColor redColor].CGColor;
    strokeAnim.toValue           = (id) [UIColor greenColor].CGColor;
    strokeAnim.duration          = 3.0;
    strokeAnim.repeatCount       = 0;
    strokeAnim.autoreverses      = YES;
    [shapeLayer addAnimation:strokeAnim forKey:@"animateStrokeColor"];

}

それがあなたのために働くかどうか教えてください...

于 2012-08-25T15:18:43.547 に答える