Iphone SDK - Animating Subview

14,842

Solution 1

If I understand what your explaining, I had a very similar problem the other day.

What happening on the first load is that viewDidLoad fires first. loading the nib file takes a bit more time than it takes for the viewWillAppear to fire itself.

What we're getting is a nib loads after the viewWillApper already retired.

On any load after that, the viewDidLoad will not fire, letting the viewWillAppear to do its loyal flipping job.

What to do?
First, try to change your code to use "viewDidAppear". That should help, but you have to see if it looks good.

Another option (ugly one, I know) is to have a call to checkOptionsVisible on the viewDidLoad too.
If non of that help, I would consider a timer as a hack - if the requirements allow it.

I hope that make you closer to solve the problem.

Solution 2

Hmm, I don't think the viewWillAppear message is getting sent the first time. There are two things that I read in the SDK. You should call super inside that message and there is a big warning that may apply to your first time:

Warning: If the view belonging to a view controller is added to a view hierarchy directly, the view controller will not receive this message. If you insert or add a view to the view hierarchy, and it has a view controller, you should send the associated view controller this message directly. Failing to send the view controller this message will prevent any associated animation from being displayed.

Ultimately, I would run through the debugger and make sure that viewWillAppear message is being sent when you think it is.

Share:
14,842
Dutchie432
Author by

Dutchie432

There is a lot I could write here. Generally speaking, I am a (90% self-taught) computer programmer specializing in business related software utilizing .NET, PHP, JS, AJAX, jQuery, and XCode.

Updated on June 04, 2022

Comments

  • Dutchie432
    Dutchie432 almost 2 years

    I have a View that has a UIWebView, and an OptionsPane (Custom UIViewController with Custom view).

    I want when the view is shown, for the options pane (located on the top of the main view) to FLIP into place. I am using the code, and I am getting a strange result.

    The FIRST time the view is shown, the options pane seems to already be visible... When I hit BACK on my navController, and pull up the View again, the animation works perfectly.

    Can anyone shed some light on this topic?

    - (void)viewDidLoad {
        [super viewDidLoad];
        optionsPane=[[OptionsPaneController alloc] initWithNibName:@"OptionsPane" bundle:nil];
    }
    
    - (void)viewWillAppear:(BOOL)animated {
    
        [optionsPane.view removeFromSuperview];
        [self checkOptionsVisible];
    }
    
    -(void)checkOptionsVisible{
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.5]; 
        [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:[optionsPane view] cache:YES];
        [[self view] addSubview:[optionsPane view]];
        [theWebView setFrame:CGRectMake(0,87,320,230)];
        [[optionsPane view] setFrame:CGRectMake(0,0,320,87)];
        [UIView commitAnimations];      
    }
    
  • Dutchie432
    Dutchie432 almost 15 years
    Very interesting... I must have missed that note. Let me play around with it a bit and let you know what the story is as I do BELIEVE it is being called.
  • Dutchie432
    Dutchie432 almost 15 years
    It's firing the first time, alright - as well as all of the subsequent times.
  • Boiler Bill
    Boiler Bill almost 15 years
    bummer, I was hoping that would help
  • Dutchie432
    Dutchie432 almost 15 years
    The difference is that I have BOTH views displaying at the same time, one that is about 100 pixels high directly above the other - and I am not switching between them - I am just having the top view "appear" in a flipping manner.
  • Corey Floyd
    Corey Floyd almost 15 years
    hmm... then just make a "fake" front view that is blank and also 100 pixels high. Switch between that and the option pane. That should solve your issue.
  • Dutchie432
    Dutchie432 almost 15 years
    I tried doing something like NSLog(@"%d",optionsPane.view.bounds.size.height); immediately after I set optionsPane=[[optionsPane alloc] init]; these get called before the optionsPane view is added as a subview. Still to no avail. I am starting to wonder if I should just scrap the animation - it really is non essential.
  • Dutchie432
    Dutchie432 almost 15 years
    I'm going to need to try this in a separate project before 'ripping apart' my current code to see if this works. I will keep you posted.
  • Skirwan
    Skirwan almost 15 years
    Okay, so much for that idea... Here's a new one: The only thing that's different the second time through is that [optionsPane.view removeFromSuperview] is actually doing something. Have you tried adding the optionsPane.view to self.view in viewDidLoad? It's possible that the no-op removeFromSuperview is somehow preventing the animation system from recognizing that the subsequent addSubview is animatable.
  • Dutchie432
    Dutchie432 almost 15 years
    Interesting... moving the function call to viewDidAppear causes some really funky effects, and not quite what I'm trying to do... I will need to play around with the timer option and get back to you. Thanks!
  • Corey Floyd
    Corey Floyd almost 15 years
    Good luck. For encouragement, this snippet absolutely DOES work in a production project. Whether it will work with just a partial subview, you'll find out when you test it.
  • David Salzer
    David Salzer almost 15 years
    Thanks for commenting it out. I'm looking forward to hear what worked for you (and probably work for me too in the end :) ).
  • Dutchie432
    Dutchie432 almost 15 years
    Im really not sure I need to do all of this... After studying this code in preparation to implement it - this code really transitions between two UIViews.. I am trying to have a UIView show as an object on another UIView, if you will... Let me know if I'm missing something...
  • Dutchie432
    Dutchie432 almost 15 years
    I do believe I previously tried that. I implements that line again and there is no visible change whatsoever... I appreciate your efforts!
  • Dutchie432
    Dutchie432 almost 15 years
    Here is a strange piece of information. If, in the viewDidLoad, I set [[optionsPane view] setFrame:CGRectMake(0,0,0,0)]; - the animation of the change from (0,0,0,0) to the (0,0,320,87) specified in checkOptionsVisible is shown. So - why would THAT animate, and not the 'problem' animation?
  • Dutchie432
    Dutchie432 almost 15 years
    Oh, and on the second run-through the resize animation does NOT show, while the 'problem' animation does.