- 自定义转场动画,需要遵守
UIViewControllerAnimatedTransitioning
协议,该协议有两个方法:
- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext;
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext;
第一个方法返回转场执行的时间(动画执行的时间),第二个方法是处理转场动画(想要实现什么样的转场动画,在这个方法中实现便可).
这里介绍几个背景知识:fromVC,toVC,fromView,toView,containerView
如果 A 视图控制器present到 B ,则 A 为 fromVC , A.view 为 fromView , B 为 toVC , B.view 为 toView ; 如果 B 视图控制器dismiss到 A , 则 B 为 fromVC , B.view 为 fromView , A 为 toVC , A.view 为 toView ; containerView:容器视图,所有的动画都要在容器视图上完成.
注:动画执行完成必须执行[transitionContext completeTransition:YES]
;
下面介绍转场动画的实现步骤:
- 创建继承自
NSObject
的类,并遵守UIViewControllerAnimatedTransitioning
协议,并实现协议中的两个方法. - 让presened视图控制器 遵守
UIViewControllerTransitioningDelegate
协议,设置代理为自己(self.transitioningDelegate = self;
),并实现其中的两个方法:
- (nullable id < UIViewControllerAnimatedTransitioning >)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source;
- (nullable id < UIViewControllerAnimatedTransitioning >)animationControllerForDismissedController:(UIViewController *)dismissed;
两个方法的返回值类型都为 id < UIViewControllerAnimatedTransitioning >
,只需要返回自定义动画类对应的方法即可.
第一个方法返回的是present时执行的动画,第二个方法返回的是dismiss时执行的动画.
代码如下:
.h 文件如下:
typedef NS_ENUM(NSInteger, DDPresentOneTransitionType) {
DDPresentOneTransitionTypePresent = 0,
DDPresentOneTransitionTypeDismiss
};
@interface DDPresentOneTransition : NSObject<UIViewControllerAnimatedTransitioning>
+ (instancetype)transitionWithTransitionType:(DDPresentOneTransitionType)type;
- (instancetype)initWithTransitionType:(DDPresentOneTransitionType)type;
@end
定义枚举,用来区分是presen还是dismiss,定义初始化方法,一个类方法,一个实例方法.
.m 文件如下:
```objective-c
@interface DDPresentOneTransition ()
@property (nonatomic, assign)DDPresentOneTransitionType type;
@property (nonatomic, strong) UIImageView *originalIV;//存储点击位置图片的ImageView
@property (nonatomic, strong) UIImageView *targetIV;//存储将要显示位置图片的ImageView
@end
@implementation DDPresentOneTransition
+ (instancetype _Nonnull )transitionWithTransitionType:(DDPresentOneTransitionType)type imageView:(UIImageView *_Nonnull)imageView
{
return [[self alloc] initWithTransitionType:type imageView:imageView];
}
- (instancetype _Nonnull )initWithTransitionType:(DDPresentOneTransitionType)type imageView:(UIImageView *_Nonnull)imageView
{
self = [super init];
if (self) {
_type = type;
_originalIV = imageView;
}
return self;
}
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
if (_type == DDPresentOneTransitionTypePresent) {
return 0.25;
}else{
return 0.25;
}
}
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
switch (_type) {
case DDPresentOneTransitionTypePresent:
[self presentAnimateTransition:transitionContext];
break;
case DDPresentOneTransitionTypeDismiss:
[self dismissAnimateTransition:transitionContext];
break;
default:
break;
}
}
- (void)presentAnimateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIView *containerView = [transitionContext containerView];
containerView.backgroundColor = [UIColor whiteColor];
UIView *tempView = [self.originalIV snapshotViewAfterScreenUpdates:YES];
tempView.frame = [self.originalIV convertRect:self.originalIV.bounds toView:containerView];
self.originalIV.hidden = YES;
toVC.view.alpha = 0;
[containerView addSubview:toVC.view];
[containerView addSubview:tempView];
//290*121
UIImage *image = self.originalIV.image;
CGFloat imageH = ceil(image.size.height);
CGFloat imageW = ceil(image.size.width);
CGFloat height = ceil(kScreenWidth*imageH/imageW);
[UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
tempView.frame = CGRectMake(0, 0, kScreenWidth, height);
fromVC.view.alpha = 0;
} completion:^(BOOL finished) {
toVC.view.alpha = 1;
tempView.hidden = YES;
[transitionContext completeTransition:YES];
}];
}
- (void)dismissAnimateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIView *containerView = [transitionContext containerView];
containerView.backgroundColor = [UIColor whiteColor];
UIView *tempView = containerView.subviews.lastObject;
self.originalIV.hidden = YES;
fromVC.view.hidden = YES;
tempView.hidden = NO;
toVC.view.alpha = 0;
[containerView insertSubview:toVC.view atIndex:0];
[UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
tempView.frame = [self.originalIV convertRect:self.originalIV.bounds toView:containerView];
toVC.view.alpha = 1;
} completion:^(BOOL finished) {
self.originalIV.hidden = NO;
tempView.hidden = YES;
[tempView removeFromSuperview];
[transitionContext completeTransition:YES];
}];
}