有时候我们需要自定义navigationController push和pop界面切换动画,用到的代码如下:
For Push 一、利用UIView :
kkkViewController *VC = [[kkkViewController alloc] init];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationDuration:1];
[self.navigationController pushViewController:VC animated:YES];
// 几种动画的样式(枚举值)
UIViewAnimationTransitionNone,
UIViewAnimationTransitionFlipFromLeft,
UIViewAnimationTransitionFlipFromRight,
UIViewAnimationTransitionCurlUp,
UIViewAnimationTransitionCurlDown,
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.navigationController.view cache:NO];
[UIView commitAnimations];
For Push 二、利用CATransition实现动画 :
CATransition *transition = [CATransition animation];
transition.duration = 1.0f;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = @"cube";
transition.subtype = kCATransitionFromRight; // 这里可以选择四种
transition.delegate = self;
[self.navigationController.view.layer addAnimation:transition forKey:nil];
kkkViewController *VC = [[kkkViewController alloc] init];
[self.navigationController pushViewController:VC animated:YES];
其中的动画类型有:
transition.type = kCATransitionFade;
transition.type = kCATransitionPush;
transition.type = kCATransitionReveal;
transition.type = kCATransitionMoveIn;
transition.type = @"cube";
transition.type = @"suckEffect";
// 页面旋转
transition.type = @"oglFlip";
//水波纹
transition.type = @"rippleEffect";
transition.type = @"pageCurl";
transition.type = @"pageUnCurl";
transition.type = @"cameraIrisHollowOpen";
transition.type = @"cameraIrisHollowClose";
For Pop 一、利用UIView :
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0.75];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.navigationController.view cache:NO];
[UIView commitAnimations];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelay:0.375];
[self.navigationController popViewControllerAnimated:NO];
[UIView commitAnimations];
For Pop 二、利用CATransition :
CATransition* transition = [CATransition animation];
transition.duration = 0.5;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionFade; //kCATransitionMoveIn; //, kCATransitionPush, kCATransitionReveal, kCATransitionFade
//transition.subtype = kCATransitionFromTop; //kCATransitionFromLeft, kCATransitionFromRight, kCATransitionFromTop, kCATransitionFromBottom
[self.navigationController.view.layer addAnimation:transition forKey:nil];
[[self navigationController] popViewControllerAnimated:NO];