[iOS开发]SKTransitonAdvance 转场动画的手

2019-01-11  本文已影响11人  codeTao

SKTransitonAdvance 转场动画的手势交互

一.实现转场动画需要封装两个类:

二.转场动画管理者

实现步骤:

//系统给出一个切换上下文,我们根据上下文环境返回这个切换所需要的花费时间
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
}
//在进行切换的时候将调用该方法,我们对于切换时的UIView的设置和动画都在这个方法中完成
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext { }
//通过viewControllerForKey取出转场前后的两个控制器,这里toVC就是vc1、fromVC就是vc2
UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];

三.动画控制器需要遵守协议, 实现方法

- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source;

- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed;
- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC {}

四.手势管理者

-(void)updateInteractiveTransition:(CGFloat)percentComplete;
更新百分比,一般通过手势识别的长度之类的来计算一个值,然后进行更新。

-(void)cancelInteractiveTransition; 报告交互取消,返回切换前的状态
–(void)finishInteractiveTransition; 报告交互完成,更新到切换后的状态
- (nullable id <UIViewControllerInteractiveTransitioning>)interactionControllerForPresentation:(id <UIViewControllerAnimatedTransitioning>)animator;

- (nullable id <UIViewControllerInteractiveTransitioning>)interactionControllerForDismissal:(id <UIViewControllerAnimatedTransitioning>)animator;

由于笔者水平有限,文中如果有错误的地方,或者有更好的方法,还望大神指出。
附上本文的所有 demo 下载链接,【GitHub】
如果你看完后觉得对你有所帮助,还望在 GitHub 上点个 star。赠人玫瑰,手有余香。

上一篇下一篇

猜你喜欢

热点阅读