Search code examples
iphoneiosipaduibuttoncore-animation

UIButtons will not fade out during animation block


I have a set of UIButtons and UILabels that I want to fade out when one button is selected and it is the correct button.

I've tried a number of things (commented them in the code block) and only the UILabels fade out. What am I missing here?

-(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];

I have since cleaned up the method and utilize only one type of animation block. The UIButton still will not fade out, but the label does. Here is what I have as the animation block:

        [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];

Solution

  • I couple things I notice off the bat. You are combining two types of animation techniques. Old and new:

    Either do:

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:1.5];
    
    buttonA.alpha = 0;
    
    [UIView commitAnimations];
    

    OR (and preferred as it is the "modern" way)

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

    Combining the two, as you have, would probably cause the block to be called many times as the first animation technique interpolated the second. Basically, putting many many animations in the queue, fading the button to 0 VERY fast.

    Additionally, by default (using the block at least) user interaction is disabled by default. No need to do it explicitly.