美文网首页
关于自定义push动画

关于自定义push动画

作者: 一笔春秋 | 来源:发表于2018-08-01 16:30 被阅读7次

我创建PhotoViewTransition对象来实现以下方法:

@interface PhotoViewTransition()

@property (assign, nonatomic) ViewTransitionType type;

@end

1、添加代理UINavigationControllerDelegate

- (id)navigationController:(UINavigationController*)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController*)fromVC toViewController:(UIViewController*)toVC{

    if (operation == UINavigationControllerOperationPush) {

        return [PhotoViewTransition transitionWithType:ViewTransitionTypePush];

    }else{

        if (![fromVC isKindOfClass:NSClassFromString(@"SCPictureViewController")]) {

            returnnil;

        }

        return [PhotoViewTransition transitionWithType:ViewTransitionTypePop];

    }

}

2、添加代理UIViewControllerAnimatedTransitioning

- (void)animateTransition:(nonnullid)transitionContext {

    switch (self.type) {

        case ViewTransitionTypePush:

            [self pushAnimation:transitionContext];

            break;

        default:

            [self popAnimation:transitionContext];

            break;

    }

}

- (NSTimeInterval)transitionDuration:(nullableid)transitionContext {

    if (self.type == SCPhotoViewTransitionTypePush) {

        return 1;

    }else{

        return 1;

    }

}

3实现pop和push的动画

- (void)pushAnimation:(id)transitionContext {

    PickViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];

    PictureViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];

    UIView*containerView = [transitionContext  containerView];

        UIView *tempBgView = [[UIView alloc]  initWithFrame:containerView.bounds];

        tempBgView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0];

        UIImageView *tempView = [[UIImageView alloc] initWithImage:toVC.pictureV.image];

        tempView.clipsToBounds =YES;

        PictureCollectionViewCell *fromCell = [fromVC currentCell];

        [tempBgView addSubview:tempView];

        [fromVC.view addSubview:tempBgView];

        [containerView addSubview:fromVC.view];

        [containerView addSubview:toVC.view];

        toVC.pictureV.hidden =YES;

        toVC.view.backgroundColor = [UIColor clearColor];

        // 弹簧动画,参数分别为:时长,延时,弹性(越小弹性越大),初始速度

        [UIView animateWithDuration:[selftransitionDuration:transitionContext] delay:0usingSpringWithDamping:0.5finitialSpringVelocity:0.1options:option animations:^{

。。。。。。

        } completion:^(BOOLfinished) {

。。。。。。

            [transitionContext completeTransition:YES];

        }];

    }];

}

- (void)popAnimation:(id)transitionContext {

   。。。。

}

相关文章

网友评论

      本文标题:关于自定义push动画

      本文链接:https://www.haomeiwen.com/subject/phsrvftx.html