// // TOCropViewControllerTransitioning.m // // Copyright 2015 Timothy Oliver. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to // deal in the Software without restriction, including without limitation the // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or // sell copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR // IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #import "TOCropViewControllerTransitioning.h" #import @implementation TOCropViewControllerTransitioning - (NSTimeInterval)transitionDuration:(id )transitionContext { return 0.45f; } - (void)animateTransition:(id )transitionContext { UIView *containerView = [transitionContext containerView]; UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey]; UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey]; UIViewController *cropViewController = (self.isDismissing == NO) ? toViewController : fromViewController; UIViewController *previousController = (self.isDismissing == NO) ? fromViewController : toViewController; cropViewController.view.frame = containerView.bounds; previousController.view.frame = containerView.bounds; UIImageView *imageView = nil; if ((self.isDismissing && !CGRectIsEmpty(self.toFrame)) || (!self.isDismissing && !CGRectIsEmpty(self.fromFrame))) { imageView = [[UIImageView alloc] initWithImage:self.image]; imageView.frame = self.fromFrame; [containerView addSubview:imageView]; } if (self.isDismissing == NO) { [containerView addSubview:cropViewController.view]; [containerView bringSubviewToFront:imageView]; } else { [containerView insertSubview:previousController.view belowSubview:cropViewController.view]; } if (self.prepareForTransitionHandler) self.prepareForTransitionHandler(); cropViewController.view.alpha = (self.isDismissing ? 1.0f : 0.0f); if (imageView) { [UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0.0f usingSpringWithDamping:1.0f initialSpringVelocity:0.7f options:0 animations:^{ imageView.frame = self.toFrame; } completion:^(BOOL complete) { [imageView removeFromSuperview]; }]; } [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{ cropViewController.view.alpha = (self.isDismissing ? 0.0f : 1.0f); } completion:^(BOOL complete) { [self reset]; [transitionContext completeTransition:![transitionContext transitionWasCancelled]]; }]; } - (void)reset { self.image = nil; self.fromFrame = CGRectZero; self.toFrame = CGRectZero; self.prepareForTransitionHandler = nil; } @end