该项目一共两个界面,第一个的只有一个SystemAnimationViewController只有UICollecitonView,第二个界面ImgDetailViewController只有一个UIImageView,代码简单,这里不做陈述
下面是转场动画的所有代码:
//
// ModelAnimationDelegate.m
// Demo
//
// Created by Apple on 2018/11/8.
// Copyright © 2018年 cqytjr. All rights reserved.
// #import "ModelAnimationDelegate.h"
#import "SystemAnimationViewController.h"
#import "SystemAnimaitonCollectionViewCell.h"
#import "ImgDetailViewController.h"
/*
1.描述ViewController转场的:
UIViewControllerTransitioningDelegate自定义模态转场动画时使用
,UINavigationControllerDelegate 自定义navigation转场动画时使用。,
UITabBarControllerDelegate 自定义tab转场动画时使用。
2.定义动画内容的
UIViewControllerAnimatedTransitioning,UIViewControllerInteractiveTransitioning
3.表示动画上下文的
UIViewControllerContextTransitioning
*/
@interface ModelAnimationDelegate ()< UIViewControllerAnimatedTransitioning,UIViewControllerTransitioningDelegate>
@property (nonatomic, assign) BOOL isPresentAnimationing;
@end @implementation ModelAnimationDelegate #pragma mark UIViewControllerAnimatedTransitioning // 视图弹出
- (void)presentViewAnimation:(id <UIViewControllerContextTransitioning>)transitionContext { // 获取容器view
UIView *containerView = [transitionContext containerView];
// 获取目标view
UIView *destinationView = [transitionContext viewForKey:UITransitionContextToViewKey];
destinationView.alpha = 0;
// 将目标view添加到容器view
[containerView addSubview:destinationView]; // 获取目标vc
ImgDetailViewController *destinationVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
NSIndexPath *indexPath = destinationVC.indexPath; // 获取来源vc
UINavigationController *naVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
SystemAnimationViewController *sourceVC = (SystemAnimationViewController *)naVC.topViewController;
// 获取来源view
UICollectionView *collectionView = sourceVC.collectionView;
SystemAnimaitonCollectionViewCell *selectedCell = (SystemAnimaitonCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
// 获取动画开始位置大小
CGRect startFrame = [collectionView convertRect:selectedCell.frame toView:[UIApplication sharedApplication].keyWindow]; UIImageView *animationImgView = [[UIImageView alloc] initWithFrame:startFrame];
animationImgView.image = selectedCell.img.image;
animationImgView.contentMode = UIViewContentModeScaleAspectFit;
animationImgView.clipsToBounds = YES;
[containerView addSubview:animationImgView]; // 获取动画结束位置大小
CGRect endFrame = destinationVC.img.frame; // 执行过渡动画
[UIView animateWithDuration:1.0 animations:^{
animationImgView.frame = endFrame;
NSLog(@"-----:%f %f %f %f",startFrame.origin.x,startFrame.origin.y,startFrame.size.width,startFrame.size.height);
NSLog(@"-----:%f %f %f %f",endFrame.origin.x,endFrame.origin.y,endFrame.size.width,endFrame.size.height); } completion:^(BOOL finished) {
[transitionContext completeTransition:YES];
[UIView animateWithDuration:1.0 animations:^{
destinationView.alpha = 1.0;
} completion:^(BOOL finished) {
[animationImgView removeFromSuperview];
}];
}]; } // 视图消失
- (void)dismissViewAnimation:(id <UIViewControllerContextTransitioning>)transitionContext { // 获取容器view
UIView *containerView = [transitionContext containerView];
// 获取目标view
UIView *destinationView = [transitionContext viewForKey:UITransitionContextToViewKey];
destinationView.alpha = 0;
// 将目标view添加到容器view
[containerView addSubview:destinationView]; UINavigationController *naVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
// 获取目标vc
SystemAnimationViewController *destinationVC = naVC.topViewController;
// NSIndexPath *indexPath = destinationVC.indexPath;
//
// 获取来源vc ImgDetailViewController *sourceVC = (ImgDetailViewController *)[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
NSIndexPath *indexPath = sourceVC.indexPath;
// 获取来源view
UICollectionView *collectionView = destinationVC.collectionView;
SystemAnimaitonCollectionViewCell *selectedCell = (SystemAnimaitonCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath]; // 获取动画开始位置大小
CGRect startFrame = sourceVC.img.frame; UIImageView *animationImgView = [[UIImageView alloc] initWithFrame:startFrame];
animationImgView.image = selectedCell.img.image;
animationImgView.contentMode = UIViewContentModeScaleAspectFill;
animationImgView.clipsToBounds = YES;
[containerView addSubview:animationImgView];
animationImgView.contentMode = UIViewContentModeScaleAspectFit;
animationImgView.clipsToBounds = YES; // 获取动画结束位置大小
CGRect endFrame = [collectionView convertRect:selectedCell.frame toView:[UIApplication sharedApplication].keyWindow]; // 执行过渡动画
[UIView animateWithDuration:1.0 animations:^{
animationImgView.frame = endFrame; NSLog(@"-----:%f %f %f %f",startFrame.origin.x,startFrame.origin.y,startFrame.size.width,startFrame.size.height);
NSLog(@"-----:%f %f %f %f",endFrame.origin.x,endFrame.origin.y,endFrame.size.width,endFrame.size.height);
sourceVC.view.hidden = YES; } completion:^(BOOL finished) {
[transitionContext completeTransition:YES]; }];
[UIView animateWithDuration:1.0 animations:^{
destinationView.alpha = 1.0; } completion:^(BOOL finished) {
[animationImgView removeFromSuperview];
}];
} - (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext {
return 10.0;
} - (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext {
_isPresentAnimationing ? [self presentViewAnimation:transitionContext] : [self dismissViewAnimation:transitionContext]; } #pragma mark UIViewControllerTransitioningDelegate - (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
_isPresentAnimationing = YES;
return self;
} - (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
_isPresentAnimationing = NO;
return self; } @end