5

私の viewDidLoad メソッドでは、ビューの左側、画面外にボタンを配置します。

次に、次の 2 つの方法を使用してアニメーション化します。

-(void) viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    [self showButton];
}

そして方法:

-(void) showButton {
    [myButton setTitle:[self getButtonTitle] forState:UIControlStateNormal];

    // animate in
    [UIView beginAnimations:@"button_in" context:nil];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationDone)];
    [UIView setAnimationDuration:1.0];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [myButton setFrame:kMyButtonFrameCenter]; // defined CGRect
    [UIView commitAnimations];
}

ボタンはすぐに表示され、アニメーションは表示されません。さらに、animationDone セレクターがすぐに呼び出されます。

ボタンが画面にアニメーション化されないのはなぜですか?

編集:これは、viewDidAppear でアニメーションを開始しようとすることに関係している必要があります...

4

2 に答える 2

6

アニメーション コードを試してみましたが、正常に動作します。

ボタンの初期フレームはどこに設定しますか? kMyButtonFrameCenterアニメーションを開始する前にボタンのフレームを誤って設定した可能性はありますか? これで、animationDone セレクターがすぐに呼び出される理由が説明できます。

動作するコードは次のとおりです。

-(void) showButton {
    UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [myButton setTitle:@"test" forState:UIControlStateNormal];
    myButton.frame = CGRectMake(-100.0, 100.0, 100.0, 30.0);
    [self.view addSubview:myButton];

    // animate in
    [UIView beginAnimations:@"button_in" context:nil];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationDone)];
    [UIView setAnimationDuration:1.0];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [myButton setFrame:CGRectMake(100.0, 100.0, 100.0, 30.0)]; 
    [UIView commitAnimations];
}

ご覧のとおり、アニメーション コードは何も変更していません。問題はボタンのフレームだと思います。

少し話がずれます: iOS 4 未満のアプリをビルドしていない場合は、iOS 4.0 に付属の UIView の「ブロック付きアニメーション」を参照してください。

[UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationCurveEaseIn animations:^void{myButton.frame = kMyButtonFrameCenter} completion:^(BOOL completed){NSLog(@"completed");}];

===編集===

あなたのコメントを読んだ後、私の疑いは正しくないようです。inspire48は彼の答えで正しい方向を示しています。ボタンの配置をviewDidAppearメソッド内またはメソッド内に配置して、アニメーションを呼び出す前にshowButtonボタンが画面の外に配置されるようにする必要があります

于 2011-07-20T14:45:38.880 に答える
3

アニメーション呼び出しを viewDidAppear に入れます。viewDidLoad は、より多くのデータ型のセットアップに使用されます。アニメーションなどの視覚効果は、viewDidAppear に入れる必要があります。これを確認しました。少し待つと機能します。

于 2011-07-20T14:47:40.647 に答える