自定义转场动画
2016-01-26 14:16
423 查看
1.设置自定义样式
CLNewWebViewController *web = [[CLNewWebViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:web];
web.urlStr = model.url;
// 自定义样式
nav.modalPresentationStyle = UIModalPresentationCustom;
nav.transitioningDelegate = self;
[self presentViewController:nav animated:YES completion:nil];
2.实现UIViewControllerTransitioningDelegate代理方法。在这个方法中CoustomPresentationController是继承自UIPresentationController的自定义控制器
//这个方法是说明哪个控制器控制presentatingController、presentatedController
- (UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(UIViewController *)presenting sourceViewController:(UIViewController *)source
{
return [[CoustomPresentationController alloc] initWithPresentedViewController:presented presentingViewController:presenting];
}
>在CoustomPresentationController实现几个方法
- (void)presentationTransitionWillBegin // 在这个方法中设置presentedView的尺寸,也就是跳转控制器的尺寸
{
self.presentedView.frame = self.containerView.bounds;
[self.containerView addSubview:self.presentedView];
}
- (void)presentationTransitionDidEnd:(BOOL)completed
{}
- (void)dismissalTransitionWillBegin
{}
- (void)dismissalTransitionDidEnd:(BOOL)completed
{
[self.presentedView removeFromSuperview];
}
3.开始设置转场动画,在UIViewControllerTransitioningDelegate代理方法中实现下面2个方法
// 自定义PresentedController的动画
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
{ CLAnimatedTransitioning *anim = [[CLAnimatedTransitioning alloc] init];
anim.presented = YES;
return anim; // 自己遵守UIViewControllerAnimatedTransitioning协议
}
// 自定义控制器消失时的动画
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
CLAnimatedTransitioning *anim = [[CLAnimatedTransitioning alloc] init];
anim.presented = NO;
return anim;
}
>其中CLAnimatedTransitioning是继承自NSObject,并遵守UIViewControllerAnimatedTransitioning协议,实现协议的2个方法
#pragma mark - UIViewControllerAnimatedTransitioning
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
return 1;
}
// 在这个方法中实现转场动画 :modal和dismis都调用这个方法
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
if (self.presented) { // 记录哪一个是即将出现的视图哪一个是即将消失的视图
UIView *toView = [transitionContext viewForKey:UITransitionContextToViewKey];
toView.mj_y = -toView.mj_height;
[UIView animateWithDuration:1 animations:^{
toView.mj_y = 0;
} completion:^(BOOL finished) {
[transitionContext completeTransition:YES];
}];
} else {
UIView *fromView = [transitionContext viewForKey:UITransitionContextFromViewKey];
fromView.mj_y = 0;
[UIView animateWithDuration:1 animations:^{
fromView.mj_y = -fromView.mj_height;
} completion:^(BOOL finished) {
[transitionContext completeTransition:YES];
}];
}
}
CLNewWebViewController *web = [[CLNewWebViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:web];
web.urlStr = model.url;
// 自定义样式
nav.modalPresentationStyle = UIModalPresentationCustom;
nav.transitioningDelegate = self;
[self presentViewController:nav animated:YES completion:nil];
2.实现UIViewControllerTransitioningDelegate代理方法。在这个方法中CoustomPresentationController是继承自UIPresentationController的自定义控制器
//这个方法是说明哪个控制器控制presentatingController、presentatedController
- (UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(UIViewController *)presenting sourceViewController:(UIViewController *)source
{
return [[CoustomPresentationController alloc] initWithPresentedViewController:presented presentingViewController:presenting];
}
>在CoustomPresentationController实现几个方法
- (void)presentationTransitionWillBegin // 在这个方法中设置presentedView的尺寸,也就是跳转控制器的尺寸
{
self.presentedView.frame = self.containerView.bounds;
[self.containerView addSubview:self.presentedView];
}
- (void)presentationTransitionDidEnd:(BOOL)completed
{}
- (void)dismissalTransitionWillBegin
{}
- (void)dismissalTransitionDidEnd:(BOOL)completed
{
[self.presentedView removeFromSuperview];
}
3.开始设置转场动画,在UIViewControllerTransitioningDelegate代理方法中实现下面2个方法
// 自定义PresentedController的动画
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
{ CLAnimatedTransitioning *anim = [[CLAnimatedTransitioning alloc] init];
anim.presented = YES;
return anim; // 自己遵守UIViewControllerAnimatedTransitioning协议
}
// 自定义控制器消失时的动画
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
CLAnimatedTransitioning *anim = [[CLAnimatedTransitioning alloc] init];
anim.presented = NO;
return anim;
}
>其中CLAnimatedTransitioning是继承自NSObject,并遵守UIViewControllerAnimatedTransitioning协议,实现协议的2个方法
#pragma mark - UIViewControllerAnimatedTransitioning
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
return 1;
}
// 在这个方法中实现转场动画 :modal和dismis都调用这个方法
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
if (self.presented) { // 记录哪一个是即将出现的视图哪一个是即将消失的视图
UIView *toView = [transitionContext viewForKey:UITransitionContextToViewKey];
toView.mj_y = -toView.mj_height;
[UIView animateWithDuration:1 animations:^{
toView.mj_y = 0;
} completion:^(BOOL finished) {
[transitionContext completeTransition:YES];
}];
} else {
UIView *fromView = [transitionContext viewForKey:UITransitionContextFromViewKey];
fromView.mj_y = 0;
[UIView animateWithDuration:1 animations:^{
fromView.mj_y = -fromView.mj_height;
} completion:^(BOOL finished) {
[transitionContext completeTransition:YES];
}];
}
}
相关文章推荐
- 知识片段---textfield限制字符输入个数(包括汉子)
- YTU 2811: 打鱼还是晒网
- 关于地址转换
- oracle数据库表分区基本语句
- 【Boost】boost库中thread多线程详解3——细说lock_guard
- 设置字体的行距和大小颜色等
- Cygwin安装curl后 bash: curl: command not found
- Windows 无法连接到无线网络
- python读取剪切板内容
- Android自定义底部显示对话框
- 关于 回调 、 反射 和异步的一些理解---初级知识
- window批处理-3.go
- 虚方法(virtual)和抽象方法(abstract)的区别
- php 会话控制 文件上传
- Oracle数据库迁移--->从Windows到Linux
- CI框架 -- 核心文件 之 Exceptions.php
- JMeter监控服务器CPU, 内存,网络数据
- JAVA单链表基本操作
- Proxy-Style Redis集群设计
- 【Boost】boost库中thread多线程详解2——mutex与lock