3

Imビューの上部からビューの下部のランダムな位置に赤い正方形を移動し、ランダムな位置で再び上部に戻ろうとしています。アプリは[superviewLoad]への次のコードで始まります。

[redSquare setCenter:CGPointMake(25,25)];

(赤い正方形のビューを50 x 50に設定すると、左上隅の赤い正方形でアプリが起動します)。

アニメーションの最初の部分はうまく機能します。赤い四角は、ページの下部のランダムな位置にアニメーション化されます。ただし、アニメーションなしでランダムな位置にジャンプして上部に移動します。この単純なアニメーションを支援するために、私が間違っていることや他の解決策を助けてください。

 -(void)moving{

    [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationCurveEaseIn animations:^{
   int randomx = arc4random() % 295;
   [redSquare setCenter:CGPointMake(randomx,435)];


    } completion:^(BOOL finished) {

   [redSquare setCenter:CGPointMake(redSquare.frame.origin.x,25)];
   }];

}
4

3 に答える 3

4

完了ブロックは単に新しい位置を設定します。あなたはそれを上にアニメーション化していません:

代わりにこれを試してください

-(void)moving {

    [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationCurveEaseIn animations:^{
        int randomx = arc4random() % 295;
        [redSquare setCenter:CGPointMake(randomx,435)];
    } completion:^(BOOL finished) {
        [UIViewAnimateWithDuration:1.0 delay:0.0 options:UIViewAnimationCurveEaseIn animations:^{
        [redSquare setCenter:CGPointMake(redSquare.frame.origin.x,25)];
        } completion: NULL
    }];

}

編集して追加

サブレベルが混乱を招く可能性があるのは事実ですが、メソッドとは別にブロックを定義すると、生活をシンプルにし、インデントを減らすことができます。

たとえば、上記を次のように書き換えることができます。

-(void)moving {

    workBlk_t animationBlock = ^ {
        int randomx = arc4random() % 295;
        [redSquare setCenter:CGPointMake(randomx,435)];
    };

    void (^completionBlock)(BOOL finished) = ^{
        [UIViewAnimateWithDuration:1.0 delay:0.0 options:UIViewAnimationCurveEaseIn animations:^{
            [redSquare setCenter:CGPointMake(redSquare.frame.origin.x,25)];
        } completion : NULL
    };

    [UIView animateWithDuration:1.0 
                          delay:0.0 
                        options:UIViewAnimationCurveEaseIn 
                     animations:animationBlock 
                     completion:completionBlock
    }];

}
于 2012-09-25T07:45:19.717 に答える
3

たとえば、完了ブロックで新しいアニメーションを開始する必要があります。

    completion:^(BOOL finished) {
          [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationCurveEaseIn      animations:^{
          int randomx = arc4random() % 295;
          [redSquare setCenter:CGPointMake(randomx,25)];


    } completion:^(BOOL finished) {


   }];
   }];
于 2012-09-25T07:42:48.677 に答える
1

この部分は、あなたが説明していることを正確に行います:

completion:^(BOOL finished) {
   [redSquare setCenter:CGPointMake(redSquare.frame.origin.x,25)];
}

つまり、アニメーションが完了した、オブジェクトを画面の上部に戻します。これはアニメーション化されず、アニメーションが完了した後に発生するため、インスタントになります。

アニメーションが完了したら、新しいアニメーションをトリガーする必要があります。

ただし、いくつかのネストされたアニメーション ブロックを作成するには、少し面倒になる可能性があります。最もクリーンな方法は、次のように、アニメーションを戻す別のメソッドを作成し、最初のアニメーションが完了したときにそれを呼び出すことです。

-(void) moveDown{
   [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationCurveEaseIn animations:^{
       int randomx = arc4random() % 295;
       [redSquare setCenter:CGPointMake(randomx,435)];

   } completion:^(BOOL finished) {
       // First animation completed
       [self moveBack];
   }];
}

-(void) moveBack{
   [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationCurveEaseIn animations:^{
       [redSquare setCenter:CGPointMake(redSquare.frame.origin.x,25)];
   } completion:^(BOOL finished) {
       // Second animation completed
   }];
}
于 2012-09-25T07:44:12.497 に答える