您的位置:首页 > 运维架构

自定义NavigationController 的Push 和 Pop过渡动画

2015-10-30 15:43 295 查看
现在开发ios项目有两种或者可以说三种方式:

(1)纯代码

纯代码加Xib

(2)storyboard

so,自定义过渡动画的方式也有两种,但是动画的实现都是一样的。

先说一下过渡动画的实现方式:一共有三种说白了都是控制View的CATransition动画。

第一种:

代码如下:

NKYellowViewController *dst = [[NKYellowViewController
alloc]init];

[UIView
transitionWithView:self.navigationController.view

duration:1

options:UIViewAnimationOptionTransitionCrossDissolve

animations:^{
[self.navigationController
pushViewController:dst
animated:NO];
}

completion:nil];

注意:第一个参数为self.navigationController.view

第二种:

代码如下:

[UIView
transitionFromView:self.view
toView:dst.view
duration:1
options:UIViewAnimationOptionTransitionFlipFromTop
completion:^(BOOL finished) {

[self.navigationController
pushViewController:dst animated:NO];
}];
注意:第一个参数为self.view

第三种:
代码如下:

CATransition *transition = [CATransition
animation];
transition.duration =
1.0f;

transition.timingFunction = [CAMediaTimingFunction
functionWithName:kCAMediaTimingFunctionEaseIn];

transition.type =
kCATransitionMoveIn;

transition.subtype =
kCATransitionFromTop;

[self.navigationController.view.layer
addAnimation:transition
forKey:nil];

[self.navigationController
pushViewController:dst animated:YES];

动画的类型有一下几种,效果自行尝试

/*

animation.type = kCATransitionFade;

animation.type = kCATransitionPush;

animation.type = kCATransitionReveal;

animation.type = kCATransitionMoveIn;

animation.type = @"cube";

animation.type = @"suckEffect";

// 页面旋转

animation.type = @"oglFlip";

//水波纹

animation.type = @"rippleEffect";

animation.type = @"pageCurl";

animation.type = @"pageUnCurl";

animation.type = @"cameraIrisHollowOpen";

animation.type = @"cameraIrisHollowClose";

*/

接下来说一下纯代码和storyboard的分别实现
(1)纯代码
如果用代码实现的话,大部分都是写在事件处理的函数里,如一下代码下载button的事件处理函数里
push:

- (void)pushViewController:(UIButton *)button
{

NKYellowViewController *dst = [[NKYellowViewController
alloc]init];

[UIView
transitionWithView:self.navigationController.view

duration:1

options:UIViewAnimationOptionTransitionCrossDissolve

animations:^{

[self.navigationController
pushViewController:dst
animated:NO];

}

completion:nil];
}

pop:

- (void)back {

[UIView
transitionWithView:self.navigationController.view

duration:1

options:UIViewAnimationOptionTransitionCrossDissolve

animations:^{
[self.navigationController
popViewControllerAnimated:NO];
}

completion:nil];

}

***动画代码可以任意替换,push和pop不对应也可以。

(2)storyboard
界面设计如下:



具体实现:
自定义segue,继承于UIStoryBoardSegue并重写perform方法。
<1>继承关系

@interface NKSegue :
UIStoryboardSegue
<2>重写方法

- (void)perform

{

UIViewController *src =
self.sourceViewController;

UIViewController *dst =
self.destinationViewController;

[UIView
transitionWithView:src.navigationController.view

duration:1

options:UIViewAnimationOptionTransitionCrossDissolve

animations:^{

[src.navigationController
pushViewController:dst
animated:NO];

}

completion:nil];

}
<3>设置segue,在右侧面板的属性检查器中segue Class设置为自定义的Segue类



返回同样是在返回函数里实现动画代码就ok了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: