-
(void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext {
UIView *containerView = [transitionContext containerView];
UIViewController *fromController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIView *toView = toController.view;
// Enter
if (toController.isBeingPresented) {
_isEnter = YES;
self.isTransitioning = YES;
if ([self enter_configAnimateImageView]) {
containerView.backgroundColor = [UIColor clearColor];
[containerView addSubview:toView];
[containerView addSubview:self.animateImageView];
toView.alpha = 0;
[self.imageBrowser setConnentHidden:YES];
self.animateImageView.frame = CGRectMake(0, 0, 50, 50);
self.animateImageView.center = containerView.center;
[UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
toView.alpha = 1;
self.animateImageView.frame = [self enterInFrame];
} completion:^(BOOL finished) {
[self completeTransition:transitionContext isEnter:YES];
[self.animateImageView removeFromSuperview];
}];
} else {
[containerView addSubview:toView];
[self completeTransition:transitionContext isEnter:YES];
}
}// Out
if (fromController.isBeingDismissed) {
_isEnter = NO;
self.isTransitioning = YES;
UIView *fromAnimateView = [self.imageBrowser animatedView];
CGRect toFrame = fromAnimateView.frame;
if (fromAnimateView) {
CGRect fromFrame = [fromAnimateView convertRect:fromAnimateView.frame toView:fromController.view];
if (CGRectGetMidY(fromFrame) > fromController.view.center.y) {
// 下移
toFrame.origin.y = CGRectGetMaxY(fromController.view.frame);
} else if (CGRectGetMidY(fromFrame) < fromController.view.center.y) {
toFrame.origin.y = -CGRectGetHeight(fromFrame);
} else {
//渐变
toFrame = CGRectZero;
}
[UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
if (CGRectEqualToRect(toFrame, CGRectZero)) {
fromController.view.alpha = 0;
} else {
fromAnimateView.frame = toFrame;
}
fromController.view.backgroundColor = [fromController.view.backgroundColor colorWithAlphaComponent:0];} completion:^(BOOL finished) { [self completeTransition:transitionContext isEnter:NO]; }]; } else { [self completeTransition:transitionContext isEnter:NO]; }
}
} (void)completeTransition:(nullable id <UIViewControllerContextTransitioning>)transitionContext isEnter:(BOOL)isEnter {
[transitionContext completeTransition:!transitionContext.transitionWasCancelled];
self.isTransitioning = NO;
if (isEnter) {
[self.imageBrowser setConnentHidden:NO];
}
}
pragma mark - private
-
(BOOL)enter_configAnimateImageView {
WKTopicBrowserModel *model= self.imageBrowser.currentModel;
if (!model) return NO;CGSize imagesize;
FLAnimatedImage *image = [[FLAnimatedImage alloc] initWithAnimatedGIFData:model.posterImageData];
if (image) {
self.animateImageView.animatedImage = image;
imagesize = image.size;
} else {
self.animateImageView.image = [UIImage imageWithData:model.posterImageData];
imagesize = self.animateImageView.image.size;
}
self.animateImageView.frame = CGRectZero;return YES;
} -
(CGRect)enterInFrame {
CGSize imagesize;
if (self.animateImageView.animatedImage) {
imagesize = self.animateImageView.animatedImage.size;
} else {
imagesize = self.animateImageView.image.size;
}
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
CGFloat maxScale = MIN(screenSize.width/imagesize.width, screenSize.height/imagesize.height);imagesize.width = imagesize.width * maxScale; imagesize.height = imagesize.height * maxScale;
return CGRectMake((screenSize.width-imagesize.width)/2, (screenSize.height-imagesize.height)/2, imagesize.width, imagesize.height);
}
pragma mark - getter
- (FLAnimatedImageView *)animateImageView {
if (!_animateImageView) {
_animateImageView = [FLAnimatedImageView new];
_animateImageView.contentMode = UIViewContentModeScaleAspectFit;
}
return _animateImageView;
}