您的位置:首页 > 其它

自定义转场动画

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];
}];

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: