动画完成后设置新控制器

时间:2014-07-14 12:53:22

标签: ios objective-c uinavigationcontroller

当应用程序启动时,第一个SplashScreen正在显示,并且它正在用于启动画面(用于动画目的),当动画完成时,它应该推送到另一个MainViewController。 可以任何人建议我在代码中的错误在哪里或我如何解决这个问题。

这是我的.h文件

#import <UIKit/UIKit.h>
#import "SplashScreen.h"

@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
    UIImageView *splashView;
    }
@property (strong, nonatomic) UIWindow *window;

@end

这是我的.m文件

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.

    [_window addSubview:obj_SplashViewController.view];
    [_window makeKeyAndVisible];


    splashView = [[UIImageView alloc] initWithFrame:CGRectMake(0,480, 320, 50)];
    splashView.image = [UIImage imageNamed:@"icon_offer.PNG"];
    [_window addSubview:splashView];
    [_window bringSubviewToFront:splashView];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:4.0];
    [UIView setAnimationTransition:UIViewAnimationTransitionNone forView:_window cache:YES];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(startupAnimationDone:finished:context:)];
    splashView.alpha = 0.0;
    splashView.frame = CGRectMake(0, 100, 320, 50);
    [UIView commitAnimations];

    return YES;
}




- (void)startupAnimationDone:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
    [splashView removeFromSuperview];
    MainViewController *obj_MainViewController = [[MainViewController alloc]init];
    [self.window setRootViewController:obj_MainViewController];
}

1 个答案:

答案 0 :(得分:2)

尝试使用Animation Block for iOS 4 and later

  [UIView animateWithDuration:.3
                         animations:^{
                         splashView.alpha = 0.0;
                         splashView.frame = CGRectMake(0, 100, 320, 50);
                         }
                         completion:^(BOOL finished){
                             NSLog(@"completion block");
                              [splashView removeFromSuperview];
                               MainViewController *obj_MainViewController = [[MainViewController alloc]init];
                              [self.window setRootViewController:obj_MainViewController];
                         }];
    }