开发者

Have view animate when using initWithNibName

I am trying to animate my new views as they come in or go out and can't seem to figure out how to do this. I ha开发者_运维问答ve tried several different things to no avail. Here is the code on how I load them:

 (void) displayView:(int)intNewView {
NSLog(@"%i", intNewView);

[self.currentView.view removeFromSuperview];

//[self.currentView.view removeFromSuperview];
[self.currentView release];
switch (intNewView) {
    case 1:
        self.currentView = [[FirstViewController alloc] initWithNibName:@"FirstView" bundle:nil];
        break;
    case 2:
        self.currentView = [[SecondViewController alloc] initWithNibName:@"SecondView" bundle:nil];
        break;
    case 3:
        self.currentView = [[ThirdViewController alloc] initWithNibName:@"ThirdView" bundle:nil];
        break;
    case 4:
        self.currentView = [[FourthViewController alloc] initWithNibName:@"FourthView" bundle:nil];
        break;
}

[self.view addSubview:self.currentView.view];

}

Is it even possible to have them animate when loading them this way?

As always, thanks in advance for any and all help.

Geo...


If i get it right and "self" is your navigation controller, you can try [self pushViewController:currentView animated:YES];


You can make a view transition in this way.

  1. don't remove the current view, so delete the lines
    
    [self.currentView.view removeFromSuperview];
    [self.currentView release];
    
  2. the loading code is ok, but assign the new loaded controller so something else instead of self.currentView, let's say
    
    UIViewController *newVC = [[UIViewController alloc] initWithNibName:@"" bundle:nil];
    
  3. now setup a simple animation based on view transition (I will use iOS 4.x approach based on blocks, but you can still use the old-style non-block approach for backward compatibility):
    
    [UIView transitionWithView:self.view
               duration:0.5
               options:UIViewAnimationOptionTransitionFlipFromLeft
               animations:^{ [self.currentView.view removeFromSuperview]; [self.view addSubview:newView.view]; }
               completion:NULL];
    
    Note that the 3.x based approach, without blocks, requires that you insert newView.view in the view hierarchy before running the animation, which simply consists in a switching between the views at index 0 and index 1 and, at the of the animation
  4. 4. finally you assign currentView to the new view:
    
    self.currentView=newView;
    

Please don't try to cut and paste these lines of codes: they have not been checked, so consider the procedure as valid but take care when writing the code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜