Search code examples
iosobjective-canimationuiviewibaction

Moved identical code from IBAction to method--produces different behavior


I have a button that seems superfluous. I want to remove the button and place its functionality (animating a simple bar chart with associated labels) into a method that gets called upon return from a user input view controller.

It seemed simple enough to rename the existing IBAction method and put the call to the replacement method into the delegate method that handles the transition back to the original View controller. However, this produced an unexpected result.

Here's a screenshot. The button in question is the red one (Show Graph):

enter image description here

The problem is that before the change, the two labels (firstLabel and secondLabel) over the bars would appear after the bars animate into position. After the method change (the creation code is unchanged), the labels appear instantly upon return to the VC, before the bars animate.

Here's the original IBAction method attached to the button:

- (IBAction)goButtonAction:(id)sender
{

    switch (goButtonKey)
    {
        case 0:
        {

        }
            break;

        case 1:
        {
            [self handleAvsAAction];
            [self doTheMathAvsA];
            [self makeBarChart];
        }

            break;

        case 2:
        {
            [self handleCvsCAction];
            [self doTheMathCvsC];
            [self makeBarChart];
        }


        default:
            break;
    }
    [self.goButton setUserInteractionEnabled:NO];

}

And here's the new method:

- (void) showGraph
{

    switch (goButtonKey)
    {
        case 0:
        {

        }
            break;

        case 1:
        {
            [self handleAvsAAction];
            [self doTheMathAvsA];
            [self makeBarChart];
        }

            break;

        case 2:
        {
            [self handleCvsCAction];
            [self doTheMathCvsC];
            [self makeBarChart];
        }


        default:
            break;
    }
    [self.goButton setUserInteractionEnabled:NO];

}

Here's the code (from makeBarChart) that produces the bar animation and the labels. For brevity, I'm just showing one bar--the other is essential identical, except being driven by different data:

    switch (thisRiser.tag)
    {
        case 1:
        {
            [self.chartView addSubview:firstLabel];
            [firstLabel setHidden:YES];


            [UIView animateWithDuration:.6
                                  delay:.2
                                options: UIViewAnimationCurveEaseOut
                             animations:^
             {
                 // Starting state
                 thisRiser.frame = CGRectMake(35, self.chartView.frame.size.height, barWidth, 0);
                 thisRiser.backgroundColor = startColor1;

                 // End state
                 thisRiser.frame = CGRectMake(35, self.chartView.frame.size.height, barWidth, -focusBarEndHeight);
                 thisRiser.backgroundColor = endColor1;
                 thisRiser.layer.shadowColor = [[UIColor blackColor] CGColor];
                 thisRiser.layer.shadowOpacity = 0.7;
                 thisRiser.layer.shadowRadius = 4.0;
                 thisRiser.layer.shadowOffset = CGSizeMake(5.0f, 5.0f);
                 thisRiser.layer.shadowPath = [UIBezierPath bezierPathWithRect:thisRiser.bounds].CGPath;
             }
                             completion:^(BOOL finished)
             {


                 firstLabel.frame = CGRectMake(thisRiser.frame.origin.x, thisRiser.frame.origin.y - 65, barWidth, 60);
                 [firstLabel.titleLabel setFont:[UIFont boldSystemFontOfSize:12]];
                 firstLabel.titleLabel.numberOfLines = 0;
                 firstLabel.titleLabel.textAlignment = NSTextAlignmentCenter;
                 [firstLabel setTitle:[NSString stringWithFormat:@"%@\n%.2f%%\nTime--%@",focusItemName,focusItemPercent,actualDurationFocusItem] forState:UIControlStateNormal];
                 firstLabel.backgroundColor = [UIColor clearColor];
                 [firstLabel setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                 [firstLabel setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
                 [firstLabel setHidden:NO];
                 [firstLabel addTarget:self action:@selector(firstLabelAction:) forControlEvents:UIControlEventTouchUpInside];
                 [firstLabel setUserInteractionEnabled:YES];
                 [thisRiser setUserInteractionEnabled:YES];


//                 NSLog(@"Done!");
             }];
        }
            break;

It doesn't seem reasonable that such a straightforward change would cause this effect. I've tried it several times to be sure I'm not breaking anything, and it seems that I am not.

Any ideas?


Solution

  • The problem was resolved by moving the call to the code from the former button action method (refactored into showGraph) from viewDidLoad into `viewDidAppear, as suggested by @rdelmar in a comment. Many thanks!