使用Navigation Controller从一个viewController转到另一个viewController时淡出效果

时间:2012-10-20 05:36:49

标签: objective-c ios

使用导航控制器从一个viewController转到另一个时,如何实现淡入淡出效果

通常我们会推动:

DetailViewController *obj= [[DetailViewController alloc]initWithNibName:@"DetailView" bundle:nil];
[self.navigationController pushViewController:obj animated:YES];

并弹出:

UINavigationController *navController = self.navigationController;

// retain ourselves so that the controller will still exist once it's popped off
 [[self retain] autorelease];

// Pop this controller and replace with another
[navController popViewControllerAnimated:YES];

但是当我弹出或推动时我想要褪色效果.. plz建议

1 个答案:

答案 0 :(得分:0)

这不是您想要的确切代码,但您可以了解如何制作想要的动画

首先必须为推送和弹出视图传递任何动画的参数NO,并且必须提供一些像这样的自定义动画

// Flash the screen white and fade it out to give UI feedback that a still image was taken
    UIView *flashView = [[UIView alloc] initWithFrame:[[self videoPreviewView] frame]];
    [flashView setBackgroundColor:[UIColor whiteColor]];
    [[[self view] window] addSubview:flashView];

    [UIView animateWithDuration:.4f
                     animations:^{
                         [flashView setAlpha:0.f];
                     }
                     completion:^(BOOL finished){
                         [flashView removeFromSuperview];
                     }
     ];

有关更多详细信息,请以正确的方式回答并使用该块,请参阅有关SO

的其他问题的this answer

该答案的部分代码如下

推送:

MainView *nextView = [[MainView alloc] init];
[UIView animateWithDuration:0.75
                         animations:^{
                             [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
                             [super pushViewController:nextView animated:NO];
                             [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.navigationController.view cache:NO];
                         }];

对于Pop:

[UIView animateWithDuration:0.75
                         animations:^{
                             [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
                             [UIView setAnimationTransition:transition forView:self.navigationController.view cache:NO];
                         }];
[self.navigationController popViewControllerAnimated:NO];

感谢@ijordan

该问题的其他一些提示是here

This一个是你使用的一个很好的例子,没有用它做额外的编码,因为它提供了导航控制器动画的类别。