2012-06-19 5 views
5

丸い四角形の幅をアニメーション化しようとしていますが、問題はより大きな幅からより細い幅に移行するときです。アニメーションは「収差の容易なジャンプ」です。ここで(iOS)shapeLayerで丸みのある四角形をアニメーション化する方法は?

はコードです:

shapeLayer = [CAShapeLayer layer]; 
shapeRect = CGRectMake(0.0f, 0.0f, 150.0f, 200.0f); 
[shapeLayer setBounds:shapeRect]; 
[shapeLayer setPosition:CGPointMake(iniPosX, 80.0f)]; 
[shapeLayer setFillColor:[[UIColor blackColor] CGColor]]; 
[shapeLayer setStrokeColor:[[UIColor clearColor] CGColor]]; 
[shapeLayer setLineWidth:1.0f]; 
[shapeLayer setLineJoin:kCALineJoinRound]; 
[shapeLayer setOpacity:0.2]; 
path = [UIBezierPath bezierPathWithRoundedRect:shapeRect cornerRadius:15.0]; 
[shapeLayer setPath:path.CGPath]; 
[self.layer addSublayer:shapeLayer]; 

そして私は、アニメーション開始:

- (void)adjustSelectorToPosAndSize:(float)posX andWidth:(float)width 
{ 
    shapeRect = CGRectMake(0.0f, 0.0f, width, 200.0f); 
    [shapeLayer setBounds:shapeRect]; 
    [shapeLayer setPosition:CGPointMake(posX, 80.0f)]; 
    path = [UIBezierPath bezierPathWithRoundedRect:shapeRect cornerRadius:15.0]; 
    [shapeLayer setPath:path.CGPath]; 
} 

は私が間違って何をやっているの?

+0

アレクシスの訂正をありがとう。 – murb83

答えて

3

CAShapeLayerでこれを行うには、丸い四角形にしか使用しない場合、私にはあまりにも複雑に思えます。単にthe cornerRadius properlyのCALayerを使用するのはなぜですか?

cornerRadiusを設定してフレームをアニメートすると正常に動作します。

それは単にフレーム(パスではなく)

- (void)adjustSelectorToPosAndSize:(float)posX andWidth:(float)width 
{ 
    shapeRect = CGRectMake(0.0f, 0.0f, width, 200.0f); 
    [myLayer setBounds:shapeRect]; 
    [myLayer setPosition:CGPointMake(posX, 80.0f)]; 
} 

重要連鎖によって行われるアニメーション表示
myLayer = [CALayer layer]; 
shapeRect = CGRectMake(0.0f, 0.0f, 150.0f, 200.0f); 
[myLayer setBounds:shapeRect]; 
[myLayer setPosition:CGPointMake(iniPosX, 80.0f)]; 
[myLayer setBackgroundColor:[[UIColor blackColor] CGColor]]; 
[myLayer setBorderColor:[[UIColor clearColor] CGColor]]; 
[myLayer setBorderWidth:1.0f]; 
[myLayer setOpacity:0.2]; 
[myLayer setCornerRadius:15.0]; 
[self.layer addSublayer:myLayer]; 

fillColorbackgroundColorになったとstrokeColorのような特性のいくつかは名前を変更lineWidthborderColorborderWidthなどとなりました。

+0

ありがとう!しかし、アニメーションをしない、瞬時です。 – murb83

+0

私はそれを試しました。これは、デフォルトの0.3デュレーションアニメーションを使用してアニメートされます。あなたはその層で他に何かしていますか? –

+0

アニメーションをカスタマイズしたい場合は、明示的に行う必要があります –

3

CAShapeLayerのパスプロパティがアニメーション可能ですが、それは暗黙的に境界と位置のようにそうしない、明示的なアニメーションを作成する必要があります。

CABasicAnimation* anim = [CABasicAnimation animationWithKeyPath: @"path"]; 
anim.fromValue = (id) fromPath.CGPath; 
anim.toValue = (id) toPath.CGPath; 
anim.duration = 1.0; 
[layer addAnimation: anim forKey: @“resize”]; 

あなたはおそらく、すべての3つのアニメーションが一緒に起こるようにしたいので、あなたができますそれらすべては、明示的で一緒にグループ化されます

CABasicAnimation* anim1 = [CABasicAnimation animationWithKeyPath: @"position"]; 
anim1.fromValue = [NSValue valueWithCGPoint: fromPosition]; 
anim1.toValue = [NSValue valueWithCGPoint: toPosition]; 
anim1.duration = 1.0; 

CABasicAnimation* anim2 = [CABasicAnimation animationWithKeyPath: @"bounds"]; 
anim2.fromValue = [NSValue valueWithCGRect: fromBounds]; 
anim2.toValue = [NSValue valueWithCGRect: toBounds]; 
anim2.duration = 1.0; 

CABasicAnimation* anim3 = [CABasicAnimation animationWithKeyPath: @"path"]; 
anim3.fromValue = (id) fromPath.CGPath; 
anim3.toValue = (id) toPath.CGPath; 
anim3.duration = 1.0; 

CAAnimationGroup* animG = [CAAnimationGroup animation]; 
animG.animations = [NSArray arrayWithObjects: anim1, anim2, anim3, nil]; 
animG.duration = 1.0; 
[layer addAnimation: animG forKey:@"resize"]; 

あなたの形状は内容や子供を持っていない場合、あなたは代わりのCALayerを使用することができる場合があります

CALayer* rectLayer = [CALayer layer]; 
rectLayer.masksToBounds = YES; 
rectLayer.bounds = startBounds; 
rectLayer.backgroundColor = [[UIColor blackColor] CGColor]; 
rectLayer.cornerRadius = 15.0; 
[self.layer addSublayer: rectLayer]; 

// Then later implicitly animate the bounds: 
rectLayer.bounds = newBounds; 
+0

ありがとうございます!あなたのコードをテストしています。テストが終わったら教えてください。 – murb83

+0

申し訳ありませんが、うまくいきませんが、私は何か間違っていると思いますが、レイヤーは私が欲しい場所に移動しません。私はアニメーションを行うときに基準点が違うと思います。申し訳ありませんが、私の英語は良くありません。あなたがアニメーションを使用する方法は本当に私の関心事です、あなたは私にグループアニメーションの新しい方法を開いてくれました。 ^^ – murb83

関連する問題