我创建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 {
。。。。
}