1.转场动画在iOS开发中是非常重要的,现在有时间记录一下自己的学习过程。
2.模态转场过程中有几个重要的类非常重要,要实现自定义需要很好的了解它们。
UIViewControllerAnimatedTransitioning
// 自定义转场动画时间
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext{
return 0.34f;
}
// 自定义转成动画具体实现
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext{
// 到哪个vc
UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
// 从哪个vc来
UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
// 获取过渡时候的容器view
UIView *contentView = [transitionContext containerView];;
//...
}
我们看到这个协议主要有两个方法,我们可以通过实现上面的方法来自定义实现转场的过程
UIViewControllerAnimatedTransitioning UIPercentDrivenInteractiveTransition
其中UIPercentDrivenInteractiveTransition
主要是实现UIViewControllerAnimatedTransitioning
协议主要涉及到的函数
//更新转场的进度
- (void)updateInteractiveTransition:(CGFloat)percentComplete;
//取消转场
- (void)cancelInteractiveTransition;
//完成转场
- (void)finishInteractiveTransition;
UIViewControllerTransitioningDelegate协议
// 设置继承自UIPresentationController 的自定义类的属性
- (UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(UIViewController *)presenting sourceViewController:(UIViewController *)source
{
ZJPresentationController *presentVC = [[ZJPresentationController alloc]initWithPresentedViewController:presented presentingViewController:presenting];
// 设置距离顶部的高度
presentVC.height = STATUSBAR_H;
return presentVC;
}
//控制器创建执行的动画(返回一个实现UIViewControllerAnimatedTransitioning协议的类)
- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{
//创建实现UIViewControllerAnimatedTransitioning协议的类(命名为AnimatedTransitioning)
ZJPresentAnimation *animation = [[ZJPresentAnimation alloc] init];
//将其状态改为出现
animation.presented = YES;
return animation;
}
// 控制器销毁执行的动画(返回一个实现UIViewControllerAnimatedTransitioning协议的类)
- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed{
////创建实现UIViewControllerAnimatedTransitioning协议的类(命名为AnimatedTransitioning)
ZJPresentAnimation *animation = [[ZJPresentAnimation alloc] init];
//将其状态改为出现
animation.presented = NO;
NSLog(@"ZJPresentAnimation %d",self.isRight);
animation.isRight = self.isRight;
return animation;
}
// 返回一个交互的对象(实现UIViewControllerInteractiveTransitioning协议的类)
-(id<UIViewControllerInteractiveTransitioning>)interactionControllerForDismissal:(id<UIViewControllerAnimatedTransitioning>)animator{
return self.interactiveTransition;
}
可以实现自定转场的View样式
UIPresentationController
这个是系统自带的presentController,通过继承它可以实现一定的自定义
/即将出现调用
- (void)presentationTransitionWillBegin{
[super presentationTransitionWillBegin];
// 设置present视图的高度
self.presentedView.frame = CGRectMake(0, _height, SCREEN_W, SCREEN_H);
// self.presentedView.backgroundColor = [UIColor whiteColor];
self.presentedView.layer.cornerRadius = 7;
// 添加到containerView 上
[self.containerView addSubview:self.presentedView];
//self.presentedView.layer.shadowRadius = _configuration.shadowRadius;
//self.presentedView.layer.shadowOpacity = _configuration.shadowOpacity;
}
//出现调用
- (void)presentationTransitionDidEnd:(BOOL)completed{
// 如果呈现没有完成,那就移除背景 View
[super presentationTransitionDidEnd:completed];
}
//即将销毁调用
- (void)dismissalTransitionWillBegin{
[super dismissalTransitionWillBegin];
//self.containerView.backgroundColor = [UIColor redColor];
}
//销毁调用
- (void)dismissalTransitionDidEnd:(BOOL)completed{
[super dismissalTransitionDidEnd:completed];
if (completed) {
//一旦要自定义动画,必须自己手动移除控制器
[self.presentedView removeFromSuperview];
}
}
UIViewControllerContextTransitioning
这个是很重要的一个协议,转场动画的上下文定义了转场时需要的元素,转场过程中的视图属性可以通过它获取
// 到哪个vc
UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
// 从哪个vc来
UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
// 获取过渡时候的容器view
UIView *contentView = [transitionContext containerView];;
基本的改了我们已经又了一个了解,真正的实现一些功能还需要自己动手去写一些demo
网友评论