UIButtons在动画块期间不会淡出

时间:2012-01-14 14:03:45

标签: iphone ios ipad uibutton core-animation

我有一组UIButtons和UILabel,当我选择一个按钮并且它是正确的按钮时我想淡出。

我尝试了很多东西(在代码块中对它们进行了评论),只有UILabels淡出。我在这里缺少什么?

-(IBAction)answerButtonPressed:(UIButton *)sender {
NSLog(@"Game Question Answer Pressed: %i", sender.tag);
NSLog(@"%@", sender.titleLabel.text);

int selectedAnswer =0;
selectedAnswer = [question.answer intValue];

if (selectedAnswer == sender.tag) {
    NSLog(@"GameQ %@ is the correct answer", sender.titleLabel.text);

    //self.toggleView;

    [labelA setAlpha:0];
    [labelB setAlpha:0];
    [labelC setAlpha:0];
    [labelD setAlpha:0];


    /*
    [buttonA setAlpha:0];
    [buttonB setAlpha:0];
    [buttonC setAlpha:0];
    [buttonD setAlpha:0];

    [buttonA setHidden:YES];
    [buttonB setHidden:YES];
    [buttonC setHidden:YES];
    [buttonD setHidden:YES];
     */        
    [sender setAlpha:1];
    [sender setHidden:NO];

    [[UIApplication sharedApplication] beginIgnoringInteractionEvents];
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:1.5];

    [UIView animateWithDuration:2.0
                          delay:0.0
                        options: UIViewAnimationCurveEaseOut
                     animations:^{buttonA.alpha = 0;}
                     completion:nil];

    [UIView setAnimationDelegate:[UIApplication sharedApplication]];
    [UIView setAnimationDidStopSelector:@selector(endIgnoringInteractionEvents)];

    [UIView commitAnimations];

我已经清理了方法,只使用了一种动画块。 UIButton仍然不会淡出,但标签确实如此。这是我作为动画块的内容:

        [UIView animateWithDuration:2.0
                          delay:0.0
                        options: UIViewAnimationCurveEaseOut
                     animations:^{buttonA.alpha = 0;}
                     completion:nil];

    [UIView animateWithDuration:2.0
                          delay:0.0
                        options: UIViewAnimationCurveEaseOut
                     animations:^{labelA.alpha = 0;}
                     completion:nil];

1 个答案:

答案 0 :(得分:9)

我发现了一些我注意到的事情。您正在组合两种类型的动画技术。新旧:

要么:

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.5];

buttonA.alpha = 0;

[UIView commitAnimations];

OR(并且首选,因为它是“现代”方式)

[UIView animateWithDuration:2.0
                      delay:0.0
                    options: UIViewAnimationCurveEaseOut
                 animations:^{buttonA.alpha = 0;}
                 completion:nil];

将这两者结合起来,可能会导致块被多次调用,因为第一个动画技术插入了第二个。基本上,在队列中放置许多动画,将按钮淡入0非常快。

此外,默认情况下(默认情况下)(默认情况下使用块)用户交互处于禁用状态。无需明确地做到。