前言
这里面分了两个概念,交互式和转场,交互式转场动画旨在可以通过手势控制转场动画的进行,转场动画可以提供炫酷的视觉效果,让用户能够使用的很舒服;
转场动画
转场主要分为push和present,要实现这两种类型的转场动画当然也要使用不同的方式;
当需要从AController->BController时,在以push是方式进行转场时,在BController中需要遵循UINavigationControllerDelegate
- (nullable id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
animationControllerForOperation:(UINavigationControllerOperation)operation
fromViewController:(UIViewController *)fromVC
toViewController:(UIViewController *)toVC NS_AVAILABLE_IOS(7_0);
- (nullable id <UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController *)navigationController
interactionControllerForAnimationController:(id <UIViewControllerAnimatedTransitioning>) animationController NS_AVAILABLE_IOS(7_0);
其中第一个函数返回一个遵守UIViewContrllerAnimationTransitioning协议的对象,即实现动画的对象,这个动画对象是所有转场动画的关键;
第二个函数返回一个遵守UIViewControllerInteractiveTransitioning协议的对象,返回一个交互对象,即上述所提到的;
再来看但从AController -> BController是以present的方式进行转场时,则在BController中需要遵循UIViewControllerTransitioningDelegate协议并且实现如下函数:
- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source;
- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed;
- (nullable id <UIViewControllerInteractiveTransitioning>)interactionControllerForPresentation:(id <UIViewControllerAnimatedTransitioning>)animator;
- (nullable id <UIViewControllerInteractiveTransitioning>)interactionControllerForDismissal:(id <UIViewControllerAnimatedTransitioning>)animator;
其中前两个函数也是返回一个遵循UIViewContrllerAnimationTransitioning的对象,和上述描述差不多,但不一样的是,present方式的转场为present和pop都提供了函数,后两个函数同样是返回交互对象;
动画实现
接下来就最重要的动画实现部分了,上述说过需要在代理函数中返回动画对象,就是现在所说的我们要实现的动画对象,首先创建一个遵循UIViewContrllerAnimationTransitioning协议的对象,然后实现该协议重要的两个函数如下:
- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext;
// This method can only be a nop if the transition is interactive and not a percentDriven interactive transition.
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext;
其中第一个函数返回动画执行的时间,第二个函数则是做相应的动画,接下来主要讲解第二个函数,让大家来理解动画主要的实现方式:
首先我们可以看到这个函数传进来一个遵循UIViewControllerContextTransitioning协议的函数,这个对象当中包含了做动画的context,里面包含有重要的信息,其中有做动画的两个controller的信息,即fromViewController和toViewController,还有做动画的父视图,这个视图是做动画时最关键的视图,即在它上面做相应的转场动画,这些主要信息都可以从transitionContext对象中拿到。
- (void)doPushAnimation:(id<UIViewControllerContextTransitioning>)transitionContext
{
//根据key拿到两个参与转场的VC;
ViewController *fromVC = (ViewController *)[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
LCYPushToViewController *toVC = (LCYPushToViewController *)[transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
//拿到主要用于做动画的containerView;
UIView *containerView = [transitionContext containerView];
LCYCustomCollectionViewCell *cell = (LCYCustomCollectionViewCell *)[fromVC.collectionView cellForItemAtIndexPath:fromVC.currentIndexPath];
//截图用于做过渡
UIView *tempView = [cell.imageView snapshotViewAfterScreenUpdates:NO];
tempView.frame = [cell.imageView convertRect:cell.imageView.bounds toView:containerView];
//动画前各个视图的状态
cell.imageView.hidden = YES;
toVC.view.alpha = 0;
toVC.imageView.hidden = YES;
//将两个view添加到containerview上
[containerView addSubview:toVC.view];
[containerView addSubview:tempView];
//开始动画
[UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
tempView.frame = [toVC.imageView convertRect:toVC.imageView.bounds toView:containerView];
toVC.view.alpha = 1;
} completion:^(BOOL finished) {
tempView.hidden = YES;
toVC.imageView.hidden = NO;
//注意,动画完成之后一定要加上这句,因为完成转场后需要push或者pop掉参与转场的controller,以此来作为标准;
[transitionContext completeTransition:YES];
}];
}
然后再看第二个函数,返回一个交互对象,交互对象可以是任意手势的交互,这个看你们自己的需要,交互的主要核心是同步手势和动画;
这里需要先创建一个继承自UIPercentDrivenInteractivetransition的类,在该类中实现对应的手势控制动画的操作,在该类中主要有四个函数在起作用:
- (void)updateInteractiveTransition:(CGFloat)percentComplete;
- (void)cancelInteractiveTransition;
- (void)finishInteractiveTransition;
1:主要更新动画执行的时间点,用于同步动画的执行过程;
2:表示动画执行完成;
3:表示动画被取消执行;
还有一个函数是iOS10之后出来的- (void)pauseInteractiveTransition NS_AVAILABLE_IOS(10_0);
下面的代码来阐述这三个函数应当如何使用;
- (void)handleGesture:(UIPanGestureRecognizer *)panGesture
{
CGFloat transitionX = [panGesture translationInView:panGesture.view].x;
CGFloat persent = transitionX / panGesture.view.frame.size.width;
switch (panGesture.state) {
case UIGestureRecognizerStateBegan:
_isInteration = YES;
[self start];
break;
case UIGestureRecognizerStateChanged:
[self updateInteractiveTransition:persent];
break;
case UIGestureRecognizerStateEnded:
_isInteration = NO;
if (persent > 0.5) {
[self finishInteractiveTransition];
}else{
[self cancelInteractiveTransition];
}
break;
default:
break;
}
}
当使用交互式动画时,由于动画的执行会被手势所掌控,所以需要判断动画是否被取消了,然后再作出相应的操作,下面这段代码阐述了应该如何做判断;
[UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
tempView.frame = [cell.imageView convertRect:cell.imageView.bounds toView:containerView];
fromVC.view.alpha = 0;
} completion:^(BOOL finished) {
//由于加入了手势所以必须判断手势是取消了还是完成了;
[transitionContext completeTransition:![transitionContext transitionWasCancelled]];
if ([transitionContext transitionWasCancelled]) {
tempView.hidden = YES;
fromVC.imageView.hidden = NO;
//fromVC.view.alpha = 1.0;
}else{
cell.imageView.hidden = NO;
[tempView removeFromSuperview];
//注意,动画完成之后一定要加上这句,因为完成转场后需要push或者pop掉参与转场的controller,以此来作为标准;
[transitionContext completeTransition:YES];
}
}];
这里我只用了push的方式展示了转场动画的实现,present方式与其类似,所以这里不再赘述~~最后,附上笔者所写的一个交互式转场动画的demo,请戳这里
希望本文对大家有所帮助!