iphone - 繰り返し - uiviewkeyframeanimationoptions
私のプロパティをアニメーション化しないCABasicAnimation (2)
キーパスはオブジェクトの名前ではなく、プロパティのキーパスでなければなりません。
これを使って
[CABasicAnimation animationWithKeyPath:@"frame"]
これの代わりに
[CABasicAnimation animationWithKeyPath:@"layerImage.frame"]
そして、ちょうどBTW、アニメーションをレイヤーに追加すると、そのキーはアニメーション化するキープロパティを意味しません。 このアニメーションに必要なキー(名前)だけです(これはコードの最後の行を指します)
私は私のアニメーションが何が間違っているのかを理解しようとしてきましたが、まだそれを理解していません。 私はそれが本当にまっすぐ進むはずだと思うが、多くの例と文書を読んだ後でさえ、私が見逃しているものがあるだろう。
私の問題は、もともとはiPhone上で、レイヤーを自動的に(ビューで)サイズ変更することができないという事実から来ています。 ドキュメントにはそうではないと書かれていますが、SDKのレイヤーに自動サイズ変更マスクはありません。 だから私はちょっとした回避策を講じてレイヤーをアニメーション化することにしました。
私はシンプルなリサイズアニメーションを行うべきこのシンプルなコードを持っています。 値は良好で、アニメーションの開始/終了をトレースするためにデリゲートを設定します。
// I've got a property named layerImage (which is a CALayer)
- (void)animateTestWithFrame:(CGRect)value {
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"layerImage.frame"];
animation.duration = 1;
animation.fromValue = [NSValue valueWithCGRect:self.frame];
animation.toValue = [NSValue valueWithCGRect:value];
animation.removedOnCompletion = YES;
animation.delegate = self;
[self.layer addAnimation:animation forKey:@"layerImage.frame"];
}
だから、どんなアイデアですか? (コードを含むこのビューは、それが差を生む可能性がある場合、ウィンドウのサブビューのサブビューです)
---編集---
フレームはCABasicAnimationと名前付きプロパティ "frame"を介してアニメーション化できないようです。 境界を使うとき、私は奇妙な結果を得ましたが、少なくとも私は何かを得ています。 これについて調査を続けます。
だから、iPhone上でフレームがアニメーション化されないので、レイヤーの@ "bounds"と@ "position"をアニメーション化するという解決策がありました。 位置がレイヤーの中心であり、境界のサイズが中心から広がっていることを理解するまでには時間がかかりましたが、それは簡単な部分でした。
だから、私が再開したことは、
- setFrameで、boundsとpositionプロパティを持つ2つのアニメーションを作成します。
- "animation.removedOnCompletion = NO; animation.fillMode = kCAFillModeForwards;"を使用してください。 アニメーションの最後に値がリセットされないようにする
- "animationDidStop:finished:"を実装するために、デリゲートを自己に登録します。 "layerImage.bounds = [animation.toValue CGRectValue]; layerImage.position = [animation.toValue CGPointValue];"という値を設定する必要があるようです。
UIViewアニメーションシステムを直接使用することはできませんでした。なぜなら、レイヤーで必要としていたことをしていないからです。
私が "addAnimation"で持っていたレイヤーの問題を指摘してくれてありがとうtadej5553。 だから、それはどのように見えるかを見たい人のためのコードです。
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
CABasicAnimation *animation = (CABasicAnimation*)anim;
if ([animation.keyPath isEqualToString:@"bounds"]) {
layerImage.bounds = [animation.toValue CGRectValue];
} else if ([animation.keyPath isEqualToString:@"position"]) {
layerImage.position = [animation.toValue CGPointValue];
}
}
- (void)setFrame:(CGRect)value {
CGRect bounds = CGRectMake(0, 0, value.size.width, value.size.height);
if ([UIView isAnimationStarted]) {
// animate the bounds
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"bounds"];
animation.duration = [UIView animationDuration];
animation.fromValue = [NSValue valueWithCGRect:layerImage.bounds];
animation.toValue = [NSValue valueWithCGRect:bounds];
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
animation.timingFunction = [UIView animationFunction];
animation.delegate = self;
[layerImage addAnimation:animation forKey:@"BoundsAnimation"];
// animate the position so it stays at 0, 0 of the frame.
animation = [CABasicAnimation animationWithKeyPath:@"position"];
animation.duration = [UIView animationDuration];
animation.fromValue = [NSValue valueWithCGPoint:layerImage.position];
animation.toValue = [NSValue valueWithCGPoint:CGPointMake(bounds.size.width / 2, bounds.size.height / 2)];
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
animation.timingFunction = [UIView animationFunction];
animation.delegate = self;
[layerImage addAnimation:animation forKey:@"PositionAnimation"];
} else {
layerImage.frame = bounds;
}
[super setFrame:value];
}