源码阅读(一)RadialTransitionExample

来源:互联网 发布:oppo状态栏美化软件 编辑:程序博客网 时间:2024/05/18 13:42

这是一个Radial的过渡效果
源码地址 (https://github.com/apadalko/RadialTransition_objC)
源码实现的是navigationController的push和pop操作是的圆弧形过渡效果。可借鉴的是Category和委托机制巧妙的使用,对控制器和过渡逻辑实现了松耦合。
当然其中CADisplayLink来实现“监听“,过渡的完成还是取消(updateInteractiveTransition),理解还不太深刻待深究
代码封装的很好,在控制器的push和pop只暴露了调用接口,其他操作全部分离到UINavigationController+RadialTransaction,UIView+Radial,AAPTransitionDirector中了
那就step by step 的看一下作者的思路

UINavigationController+RadialTransaction

这个导航的分类,实现了对push,pop操作以及对(UIScreenEdgePanGestureRecognizer)拖拽返回的手势处理。
push和pop的思路是一样,就只看push操作吧
最终push操作都调用到这个基础方法

-(void)radialPushViewController:(UIViewController *)viewController  withDuration:(float)duration withStartFrame:(CGRect)rect  comlititionBlock:(void(^)())block{    AAPTransitionDirector * a=[[AAPTransitionDirector alloc]init];    a.duration=duration;    a.animBlock=^(id<UIViewControllerContextTransitioning> transitionContext,float time,void(^comlitBlock)() ){        UIViewController* toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];    //对toViewController.view 的圆弧形处理        [toViewController.view radialAppireanceWithStartFrame:rect duration:time andComplitBLock:comlitBlock];    };    //关键的一步    [self setDelegate:a];    [self pushViewController:viewController animated:YES];     self.delegate = nil;}

通过把过渡需要的准备工作交给了AAPTransitionDirector类。
先看一下它的头文件就知道它做了哪些事情

@interface AAPTransitionDirector : NSObject<UIViewControllerAnimatedTransitioning,UINavigationControllerDelegate,UIViewControllerInteractiveTransitioning>

AAPTransitionDirector

它做了三件事
实现导航控制器的委托方法

#pragma mark - navigation controller delegate//在push和pop操作时,告诉navigationController将以怎样的效果来呈现toVC或者fromVC-(id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC {    return self;}

实现交互返回过渡UIViewControllerInteractiveTransitioning
//在手势拖拽返回的中间过程,告诉navigationController将以怎样的效果来过渡
-(id)navigationController:(UINavigationController *)navigationController interactionControllerForAnimationController:(id)animationController {
return self.isInteractive?self:nil;
}

交互过渡过程中,常规思路是使用UIPercentDrivenInteractiveTransition配合UIScreenEdgePanGestureRecognizer,在手势状态改变过程(UIGestureRecognizerStateChanged)来更新进度(updateInteractiveTransition),过程中是完成了还是取消了相应调用
看一下文档对iOS7新增UIPercentDrivenInteractiveTransition的解释

A percent-driven interactive transition object drives the custom animation between the disappearance of one view controller and the appearance of another. It relies on a transition animator delegate—a custom object that adopts the UIViewControllerAnimatorTransitioning protocol—to set up and perform the animations.

简单解释,它是一个百分比驱动的交互对象,它的这个百分比也就对应视图切换,过渡动画的中间状态。要注意的是,它依赖于遵循UIViewControllerAnimatorTransitioning协议方法来执行动画。
具体见
(void)updateInteractiveTransition:(CGFloat)percentComplete;
- (void)cancelInteractiveTransition;
- (void)finishInteractiveTransition;
作者没有使用UIPercentDrivenInteractiveTransition,而是使用 id _context,CADisplayLink完成了一个替代它的功能。(表示没看懂~~)
实现 UIViewControllerAnimatorTransitioning委托方法

- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext;
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext;

UIView+Radial

这个类中就实现了动画效果了,见-(void)radialPushViewController:(UIViewController *)viewController withDuration:(float)duration withStartFrame:(CGRect)rect comlititionBlock:(void(^)())block,它是对toViewController.view的path完成基本动画

****省略部分代码CABasicAnimation* revealAnimation = [CABasicAnimation animationWithKeyPath:@"path"];    revealAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];    revealAnimation.fromValue = (__bridge id)(path);    revealAnimation.toValue = (__bridge id)(newPath);    revealAnimation.duration =duration;    maskLayer.path = newPath;    CGPathRelease(path);

仅作为自己学习笔记,整理思路
在github上搜索 navigation+transition+animation 学习更多过渡动画例子
有几个很不错的例子,思路很清晰建议看看,
ZFDragableModalTransition
PushBackNavigationTransition一个3D效果,感觉性能略差
InteractiveViewControllerTransitions

0 0
原创粉丝点击