1

アプリで UIButton を動的に作成しています。ボタンを表示するときは、アニメーション化したいです。このコードでボタンをアニメーション化できないようです:

UIImage *buttonBackground = [UIImage imageNamed:@"ButtonRed.png"];
for (NSInteger x = 0; x < 4; x++) {

    CGRect btnFrame = CGRectMake(x * (buttonBackground.size.width+2),
                                 y * (buttonBackground.size.height + 1),
                                 buttonBackground.size.width,
                                 buttonBackground.size.height);
    UIButton *gridButton = [[[UIButton alloc] initWithFrame: btnFrame] retain];

    [gridButton setBackgroundImage:buttonBackground forState:UIControlStateNormal];
    [buttonBackground release];

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:.5];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:gridButton cache:YES];

    [self.view addSubview:gridButton];

    [UIView commitAnimations];
    [gridButton release];
}

「forView:gridButton」を「forView:self.view」に変更すると、ビュー全体が反転しますが、個々のボタンは反転しません。個々のボタンを反転させたいだけです。

助けてくれてありがとう。

4

2 に答える 2

0

ご提案いただきありがとうございます、rpetrich。正しい方向に考えさせられました。

個々のボタンの for ループ内でアニメーションが実行されないように見えました。FOR ループ全体を beginAnimations 呼び出しでラップしようとしましたが、それもうまくいきませんでした。

私は rpetrich が提案した performSelector コマンドにあまり慣れていませんでしたが、タイマーを使用できると考えました。

動的に作成されたすべてのボタンを最初に FOR ループで非表示に設定し、次に FOR ループの外側で、アニメーションの実行中にそれらを再表示するタイマーを作成することで、最終的にボタンをアニメーション化することができました。

私が行ったコードの変更は次のとおりです。

UIImage *buttonBackground = [UIImage imageNamed:@"ButtonRed.png"];
for (NSInteger x = 0; x < 4; x++) {

    CGRect btnFrame = CGRectMake(x * (buttonBackground.size.width+2),
                                 y * (buttonBackground.size.height + 1),
                                 buttonBackground.size.width,
                                 buttonBackground.size.height);
    UIButton *gridButton = [[[UIButton alloc] initWithFrame: btnFrame] retain];

    [gridButton setBackgroundImage:buttonBackground forState:UIControlStateNormal];
    [buttonBackground release];

    // ****** I took out all animation code ******
    [self.view addSubview:gridButton];
    gridButton.hidden = YES; // <----------- ****** new code ******

    [gridButton release];
}

// **** the following code was added just outside the FOR loop to create timer that will animate buttons as they're un-hidden.
// NSTimer gridTimer is created in the header file.
gridTimer = [NSTimer scheduledTimerWithTimeInterval:.1 target:self selector:@selector(showGrid) userInfo:nil repeats:NO];

タイマーで呼び出す新しいメソッドは次のとおりです。

-(void) showGrid{
    for (UIButton *obj in self.view.subviews) {
        if ([obj isMemberOfClass:[UIButton class]]) {
            [UIView beginAnimations:nil context:NULL];
            [UIView setAnimationDuration:1];
            [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:obj cache:YES];//~~self.view cache:YES];              
            obj.hidden = NO;
            [UIView commitAnimations];
        }
    }
    [gridTimer invalidate];
}
于 2009-07-27T23:34:48.277 に答える
0

+[UIView setAnimationTransition: forView:cache:]ビューをパラメーターとして呼び出す前に、ビューが表示されている必要がありforView:ます。

ボタンを親ビューに追加してからアニメーションを呼び出すように再配置します

于 2009-07-24T16:48:17.767 に答える