自定义转场动画学习
2020-04-30 本文已影响0人
冷武橘
一、系统的转场动画
@interface UIViewController: NSObject
@property(nonatomic,assign) UIModalTransitionStyle modalTransitionStyle ;
@property(nonatomic,assign) UIModalPresentationStyle modalPresentationStyle ;
@end
-
modalPresentationStyle :最后弹出的呈现风格。仅限于UIModalPresentationFullScreen 或 UIModalPresentationCustom 这两种模式;
-
modalTransitionStyle:弹出时的过渡动画风格。默认是UIModalTransitionStyleCoverVertical.常用的:
UIModalTransitionStyleFlipHorizontal;水平翻转动画。
UIModalTransitionStylePartialCurl;翻页动画。
如果现在有A控制器present出B控制器。层级如图所示
屏幕快照 2019-03-22 下午4.08.59.png
A控制器present 出B,系统会自动生成一个UITransitionView添加window上,并将A控制器暂时从windows移除。其中UITransitionView是一个容器视图,转场动画中它扮演一个很重要的角色。A控制器presentB,其间的过渡动画其实就是在容器视图上完成的。过渡动画完成以后,先将容器视图的动画视图移除,随后再将B控制器的视图添加到容器视图上。
#pragma mark - UIViewControllerTransitioningDelegate
//1、弹出的动画由谁来做?
- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{
return self;
}
/*消失的动画由谁来做?
- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed{
return self;
}
*/
#pragma mark - UIViewControllerAnimatedTransitioning
//2.1、动画持续的时长
- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext{
return 3;
}
#pragma mark - UIViewControllerAnimatedTransitioning
//2.2、动画该怎么做
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext{
UIView *containerView = transitionContext.containerView;//
//2.2.1、将即将做动画的视图添加到容器view上
UIView *anitionView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"timg.jpeg"]];
[containerView addSubview:anitionView];
//2.2.2、实现动画的细节
anitionView.frame = CGRectMake(0, 0, 100, 100);//确定动画视图的起始位置
[UIView animateWithDuration:3 animations:^{
anitionView.frame = self.view.bounds;////确定动画视图的结束位置位置
} completion:^(BOOL finished) {
[transitionContext completeTransition:YES];//必须告诉系统过渡动画完成了,才可以继续交互。
[anitionView removeFromSuperview];//将anitionView从容器View上移除
//2.2.3、添加目标View到容器View上。默认情况下目标View不会被添加到containerView上,需要我们手动添加才可以。
UIView *toView = [transitionContext viewForKey:UITransitionContextToViewKey];
//UIViewController *vc = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
//UIView *toView = vc.view;
[containerView addSubview:toView];
}];
}
关于dismiss原理一样就不再做赘述。