私はクラス「キューブ」を持っていて、そこにメソッド回転があります。このメソッドは、左下にanchorPointを設定し、次にキューブを90度回転させます。その後、立方体を-90度回転させる別の方法が呼び出されます。これで停止セレクターの指定が完了しました。 Btw:これは特別な目的はありません。ちょうど学ぶ!iPhone-OSで実行中のアニメーションを終了するにはどうすればいいですか?
-タッチイベントで回転が呼び出されます。しかし、キューブオブジェクトがまだ回転していて、現在33.943になっているとします。ユーザーは、アニメーションが終了する前に別のタッチイベントを作成することがあります。
このタッチイベントは、即座に再び-rotateメソッドを呼び出します。何が起こるかは、変換が重なり、キューブ画像が完全に伸びて変形するということです。
新しいアニメーションが始まる前に、現在実行中のアニメーションを停止する必要があります。
UPDATE
私はアニメーションキックオフ前に、私は、これを呼び出します。また
- (void)resetAnimation {
self.layer.transform = CATransform3DIdentity; // reset the transform to the identity transform ("no rotation applied, please")
[Cube setAnimationDelegate:nil];
[Cube setAnimationDidStopSelector:NULL];
}
、私は設定を[キューブsetAnimationBeginsFromCurrentState:YES];どこでもアニメーションブロックを開始します。
アニメーションを外部からinitedされたときに、これは、次のようになります。 (注、アニメーション化キックすることができる画面上のこのクラスの5つのオブジェクトが、存在すること)
- (void)kickAnimation { // if that's called, the cube gets a kick to rotate
self.layer.transform = CATransform3DIdentity;
currentDegrees = 90; // just testing! it may also be negative
NSString *animID = [NSString stringWithFormat:@"cube_%i",self.datasetID];
if (currentDegrees > 0) {
[self rotateRight:animID finished:0 context:self];
} else if (currentDegrees < 0) {
[self rotateLeft:animID finished:0 context:self];
}
}
- (void)rotateRight:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
// 1) here the anchorPoint is set to the lower right corner
// 2) here the view's frame is repositioned so that the cube doesn't move visually after the anchorPoint changed
[self rotateByDegrees:currentDegrees animationID:animationID];
}
- (void)rotateByDegrees:(CGFloat)degrees animationID:(NSString *)animationID {
[Cube beginAnimations:animationID context:self];
[Cube setAnimationDelegate:self];
[Cube setAnimationDidStopSelector:@selector(rotateBack:finished:context:)];
[Cube setAnimationDuration:2.1f];
[Cube setAnimationBeginsFromCurrentState:YES];
CATransform3D rotatedTransform = self.layer.transform;
rotatedTransform = CATransform3DRotate(rotatedTransform, degrees * M_PI/180.0, 0.0f, 0.0f, 1.0f);
self.layer.transform = rotatedTransform;
[UIView commitAnimations];
}
- (void)rotateBack:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
currentDegrees *= -1;
[Cube beginAnimations:animationID context:self];
self.layer.transform = CATransform3DIdentity;
[Cube setAnimationDelegate:nil];
[Cube setAnimationDidStopSelector:NULL];
[Cube setAnimationDuration:2.1f];
[Cube setAnimationBeginsFromCurrentState:YES];
CATransform3D rotatedTransform = self.layer.transform;
rotatedTransform = CATransform3DRotate(rotatedTransform, currentDegrees * M_PI/180.0, 0.0f, 0.0f, 1.0f);
self.layer.transform = rotatedTransform;
currentDegrees = 0;
[UIView commitAnimations];
}
を
今はほとんど動作するようです。アニメーションキックオフが重なると、キューブはもはや変形しません。しかし、1つの事は間違っています:新しい "キック"が入って、キューブが現在回転している場合、直ちに右に回転し始めます。しかし、それは最初に戻って回転し、 "キック"(かなり奇妙に見える)を認識する)
いいえ、それはこれが起こらないことを縫い目。私はアイデンティティ変換にそれを設定できるコードのポイントを見つけました。私はそれを正しい場所に置いていませんでした。anchorPointを設定するメソッドがあり、その直後にビューが移動しないようにフレームのシフトを修正します。これは問題を引き起こしました。ここでは、アンカーポイントを変更する前にアイデンティティの変換を設定しています。完全ではありません。上記のコードを、トランスフォームの設定方法で更新しました。 – Thanks
あなたのコードを見ると、私はいくつかのことが分かります。最も重要なのは、レイヤートランスフォームを1つの場所に設定し、ビュートランスフォームを別の場所に設定することです。それらは異なる変換です。あなたは変身してそれに固執するものを選ぶべきです。あなたはアニメーション自体をどのように実行するかを示していません。 -beginAnimations:どこかでブロックしていますか?一般に、アニメーションブロックを開始し、現在の目標状態を設定してからアニメーションをコミットする必要があります。私はおそらく、変換を常に再変換するのではなく、別のモデル変数で目標を維持するでしょうが、両方とも動作することができます。 –
もう1つのこと:レイヤーを使いこなすので、modelLayerとpresentationLayerの違いに注目してください。 1つはゴール状態であり、もう1つは現在画面上にあるものです(アニメーションにいる場所)。 –