将视图控制器添加到其他视图时的自定义转换

时间:2013-12-13 11:36:12

标签: ios iphone objective-c uiviewcontroller

我使用此类别在另一个内部添加UIViewController:

@implementation UIViewController (Container)

- (void)containerAddChildViewController:(UIViewController *)childViewController parentView:(UIView *)view
{
    [self addChildViewController:childViewController];
    [view addSubview:childViewController.view];
    [childViewController didMoveToParentViewController:self];
}

- (void)containerAddChildViewController:(UIViewController *)childViewController
{
    [self containerAddChildViewController:childViewController parentView:self.view];
}

- (void)containerRemoveChildViewController:(UIViewController *)childViewController
{
    [childViewController willMoveToParentViewController:nil];
    [childViewController.view removeFromSuperview];
    [childViewController removeFromParentViewController];
}

@end

它运行良好,但我想在添加或删除UIViewController时更改默认转换(淡入/淡出)。

我试图做这样的事情,但渲染很难看。

myVC.view.alpha = 0;
[self containerAddChildViewController:myVC];
[UIView animateWithDuration:0.5 animations:^{
    myVC.view.alpha = 1;
}];

我阅读了很多关于如何为UIViewController创建自定义转换的内容,但我添加视图控制器的方式并不是主要的常用方法。所以我想就如何使用此类别更改转换提出建议。

谢谢!

1 个答案:

答案 0 :(得分:-1)

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];

    [self.window makeKeyAndVisible];

    splashView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    splashView.image = [UIImage imageNamed:@"a copy.jpg"];
    [self.window addSubview:splashView];
    [self.window bringSubviewToFront:splashView];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:5.0];
    [UIView setAnimationTransition:UIViewAnimationTransitionNone forView:self.window cache:YES];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(startupAnimationDone:finished:context:)];
    splashView.alpha = 5.0;
    splashView.frame = CGRectMake(-60, -85, 440, 635);
    [UIView commitAnimations];

    return YES;
}
- (void)startupAnimationDone:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
    [splashView removeFromSuperview];
    // Override point for customization after application launch.
    UINavigationController *navi=[[UINavigationController alloc]initWithRootViewController:self.viewController];

    self.window.rootViewController = navi;
}