0

こんにちは、画面の周りでアニメーション化する約 10 個の UIButtons があります。ユーザーがいずれかをタップすると、ボタンが拡大され、画面上の特定のポイントまで移動および縮小されます。

私はアニメーションを管理するために単一の方法を使用しています。メソッドがそのボタンでアニメーションを実行できるように、UIButton のインデックス番号をメソッドに渡します。アニメーションは正常に動作しますが、タップした前のボタンの縮小と移動のアニメーションが終了する前に 2 番目のボタンをタップすると終了します。前にタップしたボタンの縮小アニメーションは停止しますが、その移動アニメーションは完了します。

これが私のコードです。

-(void)HidingAnimation:(int)BubbleIndex{

//Obtain Bubble to hide using index number passed to method
UIButton *BubbleToHide = [Bubbles objectAtIndex:BubbleIndex - 1];
NSLog(@"BubbletoHide = %@", BubbleToHide);

[self.view bringSubviewToFront:BubbleToHide];

//Remove animations that are happening on the bubble
[BubbleToHide.layer removeAllAnimations];

//Set the position of the bubble to the current presentation layer position
CALayer *currentLayer = [BubbleToHide.layer presentationLayer];
BubbleToHide.layer.position = currentLayer.position;

//Remove the background image of the bubble
UIImage *buttonImage = [UIImage imageNamed:NULL];
[BubbleToHide setBackgroundImage:buttonImage forState:UIControlStateNormal];

//Find the position of the previously hidden bubble
UIButton *PreviousHiddenBubble = [BubblesArray objectAtIndex:BubbleIndex - 2];
CALayer *PreviousBubble = PreviousHiddenBubble.layer.presentationLayer;
PreviousHiddenBubble.layer.transform = PreviousBubble.transform;

//Calculate scaledown and position of bubble depending on game mode
CGFloat XTranslation, YTranslation, ScaleDownX, ScaleDownY;
if ([[[NSUserDefaults standardUserDefaults]valueForKey:@"Mode"]isEqualToString:@"game"]) {
    XTranslation = 44 + ((PreviousHiddenBubble.bounds.size.width/2) * (BubbleIndex -1))-10; //PreviousHiddenBubble.center.x + PreviousHiddenBubble.bounds.size.width*.35;
    YTranslation = 702;
    ScaleDownX = 0.5;
    ScaleDownY = 0.5;
}


[UIView animateWithDuration:0.2
                      delay:0
                    options:UIViewAnimationCurveLinear|UIViewAnimationOptionBeginFromCurrentState|UIViewAnimationOptionAllowAnimatedContent
                 animations:^{
                     BubbleToHide.layer.transform = CATransform3DMakeScale(4, 4, 1);
                 }
                 completion:^(BOOL finished) {
                     NSLog(@"Scale Up Animation Complete");
                     [UIView animateWithDuration:1.2
                                           delay:0
                                         options:UIViewAnimationCurveLinear|UIViewAnimationOptionOverrideInheritedDuration
                                      animations:^{
                                          BubbleToHide.transform = CGAffineTransformMakeScale(ScaleDownX, ScaleDownY);//CATransform3DMakeScale
                                          BubbleToHide.center = CGPointMake(XTranslation, YTranslation);
                                      }
                                      completion:^(BOOL finished){
                                          NSLog(@"Scale down and Transform Complete");
                                      }];
                     }];

}

スケールアップアニメーションの完成に、スケールダウンと移動アニメーションを入れます。

BubbleToHide 変数は、渡されたインデックスを使用して Bubbles 配列から取得された各 UIButton への個別の参照を保持する必要があります。

コードの上部にある NSLog で確認できますが、2 番目の UIButton がタップされ、前にタップされたボタンで scaleDown が完了していないときはいつでも。前のボタンの scaleDown はキャンセルされます。ただし、移動アニメーションは完了するので、代わりに素敵なボタンの列が表示されます。正しい位置にあるボタンがあり、scaleUp 係数と scaleDown 係数の間でサイズが固定されています。次のボタンがどれだけ速くタップされたかに応じて。

なぜこれが起こるのですか?または修正?

ありがとう。

4

1 に答える 1

0

私はそれを解決しました。

結局、ブロックアニメとは何の関係もありませんでした。それはコードでした

CALayer *PreviousBubble = PreviousHiddenBubble.layer.presentationLayer;
PreviousHiddenBubble.layer.transform = PreviousBubble.transform;

これは、前のアニメーション プレゼンテーション レイヤーの現在の位置を取得し、その変換をプレゼンテーション レイヤーの現在の変換に設定していました。スケールアニメーションがまだ進行中だったからです。その時点でスケール アニメーションがフリーズしました。

ボタンの幅に応じてボタンを均等に配置する方法を見つけようと試みました。それを行う別の方法を探す必要があります。

于 2014-03-15T21:25:44.420 に答える