一丶效果
OneViewController 的 ImageView和label 切换到TwoViewController的转场动画;
二丶原理及实现方式;
利用
实现方式:
1.OneVc上有:oneImageView;
2.TwoVc上有:twoImageView;
通过截图oneImageView生成view添加到containerView;
对containerView做过渡动画即可;
三丶代码
自己封装了一个XZB_Transition(https://github.com/k373379320/ZBTransitionTest)
使用:
//翻转OneVc
1.
#import "ZC1ViewController.h"
2.
添加UINavigationControllerDelegate>
3.
self.navigationController.delegate = self;
4.
#pragma mark
- (id )navigationController:(UINavigationController *)navigationController
animationControllerForOperation:(UINavigationControllerOperation)operation
fromViewController:(UIViewController *)fromVC
toViewController:(UIViewController *)toVC{
if ([toVC isKindOfClass:[ZC2ViewController class]]) {
XZB_Transition *transition = [XZB_Transition transitionWithViewArray:@[@[@"mainImageView",@"mainImageView"],@[@"titleLabel",@"titleLabel"]]];
return transition;
}else{
return nil;
}
}
注意:
就一个类方法;使用注意格式别错了,没有容错代码,按格式来;
由于采用kvc的方式实现,格式如下:
传入:二维数组,@[@[原图控件name1,结果图控件name1],@[原图控件name2,结果图控件name2]];
例子:
@[@[@"mainImageView",@"mainImageView"]]
+ (instancetype)transitionWithViewArray:(NSArray *)array;
四丶,代码讲解:
#import "XZB_Transition.h"
#import "UIView+MotionBlur.h"
#import "UIImage+Capture.h"
@interface XZB_Transition ()
@property (nonatomic, strong) NSArray *transitionViewArray;
@end
@implementation XZB_Transition
#pragma mark - 生命周期
#pragma mark - api
+ (instancetype)transitionWithViewArray:(NSArray *)array
{
XZB_Transition *transition = [[XZB_Transition alloc]init];
transition.transitionViewArray = array;
return transition;
}
#pragma mark - getter / setter
#pragma mark - model event
#pragma mark - view event
#pragma mark - private
/**
动画持续时间
*/
- (NSTimeInterval)transitionDuration:(id )transitionContext
{
return 0.7f;
}
- (void)animateTransition:(id )transitionContext
{
//获取从哪里来的控制器
UIViewController *fromVC = (UIViewController *)[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
//获取到哪里去的控制器
UIViewController *toVC = (UIViewController *)[transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
toVC.view.frame = [transitionContext finalFrameForViewController:toVC];
toVC.view.alpha = 0;
//转场需要动画的视图数组
NSMutableArray *animationViewsArray = [[NSMutableArray alloc] init];
//做动画的视图,注意添加顺序,层次;
UIView *containerView = [transitionContext containerView];
[containerView addSubview:toVC.view];
@autoreleasepool {
for (NSArray *startToEndViewArray in self.transitionViewArray)
{
//来源视图处理指针
UIView *fromVcAnimationView = nil;
//最终视图处理指针
UIView *toVcAnimationView = nil;
//做动画的视图指针
UIImageView *animationImageView = nil;
{
fromVcAnimationView = [fromVC valueForKey:startToEndViewArray[0]];
//截图
animationImageView = [[UIImageView alloc] initWithImage:[UIImage captureWithView:fromVcAnimationView]];
animationImageView.frame = fromVcAnimationView.frame;
[containerView addSubview:animationImageView];
}
toVcAnimationView = [toVC valueForKey:startToEndViewArray[1]];
fromVcAnimationView.hidden = YES;
toVcAnimationView.hidden = YES;
//原图,结果图,动画图
[animationViewsArray addObject:@[fromVcAnimationView,toVcAnimationView,animationImageView]];
}
}
//动画效果
NSTimeInterval duration = [self transitionDuration:transitionContext];
[UIView animateWithDuration:duration delay:0.0f usingSpringWithDamping:0.7f initialSpringVelocity:1.0f options:UIViewAnimationOptionCurveLinear animations:^{
toVC.view.alpha = 1.0;
for (NSArray *animationArray in animationViewsArray)
{
UIImageView *animationImageView = animationArray[2];
UIView *resultsView = animationArray[1];
//frame变换
animationImageView.frame = [containerView convertRect:resultsView.frame fromView:resultsView.superview];
}
} completion:^(BOOL finished) {
if (!finished)return ;
for (NSArray *animationArray in animationViewsArray)
{
UIView *fromVcAnimationView = animationArray[0];
UIView *toVcAnimationView = animationArray[1];
UIImageView *animationImageView = animationArray[2];
fromVcAnimationView.hidden = NO;
toVcAnimationView.hidden = NO;
//删除做动画view
[animationImageView removeFromSuperview];
}
//结束动画,必须调用;
[transitionContext completeTransition:!transitionContext.transitionWasCancelled];
}];
}
XZB_Transition: XZB_Transition