自定义Navigation转场动画
来源:互联网 发布:淘宝开学季海报 编辑:程序博客网 时间:2024/05/16 12:45
碰巧遇到这个需求,简单的看了下kittenyang的笔记,有些东西还是要记录一下,也方便以后研究。
简单分以下几步
1,A Push B,A要实现UINavigationControllerDelegate
2,上面的delegate中,需要实现的方法返回值是一个“转场动画”
3,转场动画是一个id<UIViewControllerAnimatedTransitioning>
4,需要实现的动画,在<UIViewControllerAnimatedTransitioning>
方法中实现
5,第二步的返回值,alloc init一个转场动画,并返回
A Push B,A要实现UINavigationControllerDelegate
这句代码务必不要忘记。 self.navigationController.delegate = self;
代理中有个方法: - (nullable id <UIViewControllerAnimatedTransitioning>)
navigationController:(UINavigationController *)navigationController
animationControllerForOperation:(UINavigationControllerOperation)operation
fromViewController:(UIViewController *)fromVC
toViewController:(UIViewController *)toVC NS_AVAILABLE_IOS(7_0);
实现了以上方法,push的过程中就会调用,返回值是一个遵循了UIViewControllerAnimatedTransitioning
代理的id值,可以理解为,返回值就是一个转场动画。通常我们可以自定义一个继承自NSObject的类并实现这个协议。
自定义转场动画
上面提到,遵循了UIViewControllerAnimatedTransitioning
代理的id值可以理解为一个转场动画,我们来定义一个这个玩意。 #import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface MagicMoveTransition :
NSObject<UIViewControllerAnimatedTransitioning>
@end
注意import了UIKit
然后.m文件中,需要实现两个方法, - (NSTimeInterval)transitionDuration:(nullable id < UIViewControllerContextTransitioning>)transitionContext;
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext;
第一个方法返回一个执行时间,第二个方法中,定义需要的动画效果。
- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext{ return 0.6f;}- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext{ //获取两个VC 和 动画发生的容器 FirstCollectionViewController *fromVC = (FirstCollectionViewController *)[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey]; SecondViewController *toVC = (SecondViewController *)[transitionContext viewControllerForKey:UITransitionContextToViewControllerKey]; //containerView包含转换过程中涉及到的view UIView *containerView = [transitionContext containerView]; //对Cell上的 imageView 截图,同时将这个 imageView 本身隐藏 CollectionViewCell *cell =(CollectionViewCell *)[fromVC.collectionView cellForItemAtIndexPath:[[fromVC.collectionView indexPathsForSelectedItems] firstObject]]; fromVC.indexPath = [[fromVC.collectionView indexPathsForSelectedItems]firstObject]; UIView * snapShotView = [cell.imageView snapshotViewAfterScreenUpdates:NO]; //将cell中图片的frame从他的父视图坐标系转到containerView坐标系中 snapShotView.frame = fromVC.finalCellRect = [containerView convertRect:cell.imageView.frame fromView:cell.imageView.superview]; cell.imageView.hidden = YES; //设置第二个控制器的位置、透明度 toVC.view.frame = [transitionContext finalFrameForViewController:toVC]; toVC.view.alpha = 0; toVC.imageViewForSecond.hidden = YES; //把动画前后的两个ViewController加到容器中,顺序很重要,snapShotView在上方 [containerView addSubview:toVC.view]; [containerView addSubview:snapShotView]; //动起来。第二个控制器的透明度0~1;让截图SnapShotView的位置更新到最新; [UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0.0f usingSpringWithDamping:0.6f initialSpringVelocity:1.0f options:UIViewAnimationOptionCurveLinear animations:^{ [containerView layoutIfNeeded]; toVC.view.alpha = 1.0; snapShotView.frame = [containerView convertRect:toVC.imageViewForSecond.frame fromView:toVC.view]; } completion:^(BOOL finished) { //为了让回来的时候,cell上的图片显示,必须要让cell上的图片显示出来 toVC.imageViewForSecond.hidden = NO; cell.imageView.hidden = NO; [snapShotView removeFromSuperview]; //告诉系统动画结束 [transitionContext completeTransition:!transitionContext.transitionWasCancelled]; }];}
很不负责的粘贴了一大段代码,具体需求具体分析吧。这样在push的controller里,代理方法就可以这样写
` - (id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC{ if ([toVC isKindOfClass:[SecondViewController class]]) { MagicMoveTransition *transition = [[MagicMoveTransition alloc]init]; return transition; }else{ return nil; }}`
加一句,苹果官方规定:所有的动画视图都必须放在transitionContext的containerView里。
搞定。
FYI
http://www.kittenyang.com/uiviewcontrollertransitioning/
https://github.com/boycechang/BCMagicTransition
- 自定义Navigation转场动画
- iOS之转场动画/自定义转场动画
- iOS自定义转场动画
- iOS自定义转场动画
- 自定义转场动画
- IOS 自定义转场动画。
- Swift自定义转场动画
- iOS自定义转场动画
- UIPresentationController自定义转场动画
- UITabBarController自定义转场动画
- Modal自定义转场动画
- iOS 自定义转场动画
- 自定义转场动画 参考
- Storyboard 自定义转场动画
- 自定义导航转场动画
- iOS自定义转场动画
- iOS - 自定义转场动画
- iOS自定义转场动画
- 排重
- Thymeleaf的表达式语言-原标题:Expression Language in Thymeleaf
- 使用H5设计表单
- Eclipse/MyEclipse中SVN代理使用及设置SVN代理方法详解
- Xcode 基础快捷键
- 自定义Navigation转场动画
- LayoutInflater的使用
- windows C++ 创建快捷方式API, 100% 能运行
- TabPagerIndicator和viewpager实现tab的切换(慕课网笔记)
- [机器学习]K近邻算法及其应用--WEKA工具
- GridView 常用属性
- Leetcode:FirstBadVersion
- windows下编译和配置boost
- 3.OC中三种线程方式总结