2011-08-16 5 views
2

私はCAShapeLayarのstrokeColorプロパティをアニメーション化しようとしています。私はドキュメンテーションを見て、それがアニメーション可能なプロパティであると述べられました。このコードは、postion.yをアニメーション化するのには機能しますが、strokeColorはアニメーション化しません。事前にコアアニメーションを使用してStrokeColorをアニメーション化する

lyr.fillColor = [[UIColor clearColor] CGColor]; 
lyr.lineWidth = 3.8; 
lyr.strokeColor = [[UIColor yellowColor] CGColor]; 
lyr.position = CGPointMake(160, 431); 
lyr.anchorPoint = CGPointMake(.5f, .5f); 
[self.view.layer addSublayer:lyr]; 

CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"strokeColor"]; 
basicAnimation.fromValue = [lyr valueForKey:@"strokeColor"]; 



basicAnimation.toValue =[NSValue valueWithPointer:[[UIColor clearColor] CGColor]]; 


basicAnimation.duration = 2; 
basicAnimation.repeatCount = 10; 
basicAnimation.autoreverses = YES; 
[lyr addAnimation:basicAnimation forKey:@"strokeColor"]; 

おかげで、 または:私は助けやアドバイス

私が使用したコードのいずれかの種類を取得させていただきます。これは動作するはず

答えて

5

、私はちょうどそれをテストして:あなたはアニメーション(その冗長)前strokeColorを設定するとあなたは同じ値にしたいので、私は、アニメーションからfromValueプロパティ行を削除

CABasicAnimation *strokeAnim = [CABasicAnimation animationWithKeyPath:@"strokeColor"]; // I changed the animation pointer name, but don't worry. 
basicAnimation.toValue = (id) [UIColor clearColor].CGColor; // Just get rid of the fromValue line, and just cast the CGColorRef to an id. 
basicAnimation.duration = 2; 
basicAnimation.repeatCount = 10; 
basicAnimation.autoreverses = YES; 
[lyr addAnimation:strokeAnim forKey:@"flashStrokeColor"]; // I like to name this key differently, so as not create confusion. 

お知らせ。 また、strokeColorのCGColorRefをidにキャストしました。これはまさにtoValueプロパティが望むものです。

+0

も ​​"fillColor"で動作します。 – nh32rg

2

受け入れられた回答コードの重要な落とし穴は、addAnimationメソッドのforKey:パラメータが、発射を防止したい暗黙のアニメーションを持つプロパティの名前である必要があることです。 CABasicAnimationで明示的なアニメーションを作成しているので、暗黙のアニメーションと新しく作成された明示アニメーションの両方が互いに干渉しないようにする必要があります。セッション424 Core Animation - Core Animation in Practice、パート1を確認のために39分でWWDC 2010 videoを参照してください。

関連する問題