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

视图必须可见,然后才能将+[UIView setAnimationTransition: forView:cache:]其作为forView:参数调用。

重新排列,以便将按钮添加到父视图,然后调用动画

于 2009-07-24T16:48:17.767 回答
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 回答