锻炼吃饭的家伙首页投稿(暂停使用,暂停投稿)

iOS自定义Navgation的push和pop动画(boss直

2016-08-11  本文已影响307人  名字能有多少字呢

前两天,写了一篇关于切换图片推拉效果的文章,在总结的时候发现还有一点没有提到,就是当页面跳转时候的动画,很多APP现在不满足于系统自带的效果,所以要重写动画,上篇中着重提到更灵活的使用scrollView,和一般动画比例的用法。而这篇着重说下如何重写系统动画。老规矩,先上图:

效果.gif

相信有不少人都用过boss直聘,当然,我也是突然来灵感,就用这个效果。
首先,需要一个collectionView。初始化,并吧cell封装出来,便于复用时的控制(这里由于资源有限,cell内容就不变了,如果想改变,可以把内容传进来,在set方法里改变本cell的内容)

UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.itemSize = CGSizeMake(kScreenSize.width - 20, 155);
layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
layout.minimumLineSpacing = 10;
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, kScreenSize.width, kScreenSize.height - 64) collectionViewLayout:layout];
_collectionView.delegate = self;
_collectionView.dataSource = self;
[_collectionView registerClass:[CoustomCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
_collectionView.backgroundColor = [UIColor groupTableViewBackgroundColor];
[self.view addSubview:_collectionView];

接下来,封装cell,在这里就不在上代码了,并给cell添加跳转事件。
封装cell之后,才是重头戏。
首先我们需要知道UIViewControllerAnimatedTransitioning协议,这个协议的实现就是负责切换的具体内容,当然可以理解为“在切换中发生了什么”。在这个协议里面有两个方法:

-(void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext{
    //获取到containerView视图
    UIView *containerView = [transitionContext containerView];
    //获取到目的视图(同样可以获取目的控制器和根源控制器)
    UIView *toView = [transitionContext viewForKey:UITransitionContextToViewKey];
    //获取到根源视图
    UIView *fromView = [transitionContext viewForKey:UITransitionContextFromViewKey];
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, kScreenSize.width, kScreenSize.height-64)];
    backView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0];
    UIView *sizeView = [[UIView alloc] initWithFrame:_frame];
    sizeView.backgroundColor = [UIColor whiteColor];
//    _isGO为变量,判断用户使用的是push还是pop
//    动画处理
    if (_isGo) {
        [containerView addSubview:backView];
        [containerView addSubview:sizeView];
        [UIView animateWithDuration:0.3 animations:^{
            fromView.transform = CGAffineTransformScale(fromView.transform, 0.9, 0.9);
            backView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.5 animations:^{
                sizeView.frame = CGRectMake(0, 64, kScreenSize.width, kScreenSize.height);
            } completion:^(BOOL finished) {
                [backView removeFromSuperview];
                [sizeView removeFromSuperview];
                toView.frame = CGRectMake(0, 0, kScreenSize.width, kScreenSize.height);
                [containerView addSubview:toView];
                [transitionContext completeTransition:YES];
            }];
        }];
    }else{
        [containerView insertSubview:toView belowSubview:fromView];
        backView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
        [containerView insertSubview:backView belowSubview:fromView];
        [UIView animateWithDuration:0.5 animations:^{
            fromView.frame = CGRectMake(0, -kScreenSize.height, kScreenSize.width, kScreenSize.height);
        }completion:^(BOOL finished) {
            [backView removeFromSuperview];
            [UIView animateWithDuration:0.3 animations:^{
                toView.transform = CGAffineTransformMakeScale(1, 1);
            } completion:^(BOOL finished) {
                [transitionContext completeTransition:YES];
            }];
        }];
    }

动画写完了,跳转问题也了,如何让跳转时走此方法,这就涉及到nav的代理了,代理中有这个方法:
-(nullable id <UIViewControllerAnimatedTransitioning>)navigationController: animationControllerForOperation: fromViewController:toViewController:
这个方法返回的就是动画,即我们刚刚实现的实现协议的类。

-(nullable id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
                                            animationControllerForOperation:(UINavigationControllerOperation)operation
                                                         fromViewController:(UIViewController *)fromVC
                                                           toViewController:(UIViewController *)toVC{
    if (operation == UINavigationControllerOperationPush) {
        _animation.isGo = YES;
    }else{
        _animation.isGo = NO;
    }
    return _animation;
}

这就是完成了自定义跳转动画
当然,还有人会用到presentViewCotroller,presentViewController动画实现都一样,当时代理时需要UIViewControllerTransitioningDelegate协议调用secondVC.transitioningDelegate = self;
代理中有两个方法,一个是跳转,还有一个是退回
-(id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:
-(id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:presentingController:sourceController:
同样返回自定义动画类即可

上一篇下一篇

猜你喜欢

热点阅读