UIViewControllerTransitioningDelegate, UIViewControllerAnimatedTransitioning

时间:2023-03-09 14:50:26
UIViewControllerTransitioningDelegate, UIViewControllerAnimatedTransitioning
 #import "ModelAnimationDelegate.h"
#import <UIKit/UIKit.h>
#import "MapVC.h"
#import "MapLiuLanViewController.h"
#import "MapCollectionViewCell.h" @interface ModelAnimationDelegate ()<UIViewControllerTransitioningDelegate, UIViewControllerAnimatedTransitioning> @property (nonatomic, assign) BOOL isPresentAnimationing; @end @implementation ModelAnimationDelegate // 视图弹出
- (void)presentViewAnimation:(id <UIViewControllerContextTransitioning>)transitionContext {
// 获取容器view
UIView *containerView = [transitionContext containerView];
// 获取目标view
UIView *destinationView = [transitionContext viewForKey:UITransitionContextToViewKey];
destinationView.alpha = ;
// 将目标view添加到容器view
[containerView addSubview:destinationView]; // 获取目标vc
MapLiuLanViewController *destinationVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
NSIndexPath *indexPath = destinationVC.indexPath; // 获取来源vc
UINavigationController *naVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
MapVC *sourceVC = (MapVC *)naVC.topViewController;
// 获取来源view
UICollectionView *collectionView = sourceVC.collectionView;
MapCollectionViewCell *selectedCell = (MapCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath]; // 获取动画开始位置大小
CGRect startFrame = [collectionView convertRect:selectedCell.frame toView:[UIApplication sharedApplication].keyWindow]; UIImageView *animationImgView = [[UIImageView alloc] initWithFrame:startFrame];
animationImgView.image = selectedCell.imgView.image;
animationImgView.contentMode = UIViewContentModeScaleAspectFill;
animationImgView.clipsToBounds = YES;
[containerView addSubview:animationImgView]; // 获取动画结束位置大小
CGFloat w = WIDTH;
CGFloat h = w / animationImgView.image.size.width * animationImgView.image.size.height;
CGFloat x = ;
CGFloat y = (HEIGHT - h) / 2.0;
CGRect endFrame = CGRectMake(x, y, w, h); // 执行过渡动画
[UIView animateWithDuration:1.0 animations:^{
animationImgView.frame = endFrame;
} completion:^(BOOL finished) {
[transitionContext completeTransition:YES];
[UIView animateWithDuration:0.5 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 = ;
// 将目标view添加到容器view
[containerView addSubview:destinationView]; // 获取目标vc
MapLiuLanViewController *destinationVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
NSIndexPath *indexPath = destinationVC.indexPath; // 获取来源vc
UINavigationController *naVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
MapVC *sourceVC = (MapVC *)naVC.topViewController;
// 获取来源view
UICollectionView *collectionView = sourceVC.collectionView;
MapCollectionViewCell *selectedCell = (MapCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath]; // 获取动画开始位置大小
CGRect startFrame = [collectionView convertRect:selectedCell.frame toView:[UIApplication sharedApplication].keyWindow]; UIImageView *animationImgView = [[UIImageView alloc] initWithFrame:startFrame];
animationImgView.image = selectedCell.imgView.image;
animationImgView.contentMode = UIViewContentModeScaleAspectFill;
animationImgView.clipsToBounds = YES;
[containerView addSubview:animationImgView]; // 获取动画结束位置大小
CGFloat w = WIDTH;
CGFloat h = w / animationImgView.image.size.width * animationImgView.image.size.height;
CGFloat x = ;
CGFloat y = (HEIGHT - h) / 2.0;
CGRect endFrame = CGRectMake(x, y, w, h); // 执行过渡动画
[UIView animateWithDuration:1.0 animations:^{
animationImgView.frame = endFrame;
} completion:^(BOOL finished) {
[transitionContext completeTransition:YES];
[UIView animateWithDuration:0.5 animations:^{
destinationView.alpha = 1.0;
} completion:^(BOOL finished) {
[animationImgView removeFromSuperview];
}];
}];
} #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;
} #pragma mark ----- UIViewControllerAnimatedTransitioning协议方法 ----- - (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext {
return 1.0;
} - (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext {
_isPresentAnimationing ? [self presentViewAnimation:transitionContext] : [self dismissViewAnimation:transitionContext];
} @end