TOCropViewControllerTransitioning.m 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. //
  2. // TOCropViewControllerTransitioning.m
  3. //
  4. // Copyright 2015 Timothy Oliver. All rights reserved.
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to
  8. // deal in the Software without restriction, including without limitation the
  9. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  10. // sell copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  17. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  20. // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
  21. // IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. #import "TOCropViewControllerTransitioning.h"
  23. #import <QuartzCore/QuartzCore.h>
  24. @implementation TOCropViewControllerTransitioning
  25. - (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext
  26. {
  27. return 0.45f;
  28. }
  29. - (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext
  30. {
  31. UIView *containerView = [transitionContext containerView];
  32. UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
  33. UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
  34. UIViewController *cropViewController = (self.isDismissing == NO) ? toViewController : fromViewController;
  35. UIViewController *previousController = (self.isDismissing == NO) ? fromViewController : toViewController;
  36. cropViewController.view.frame = containerView.bounds;
  37. previousController.view.frame = containerView.bounds;
  38. UIImageView *imageView = nil;
  39. if ((self.isDismissing && !CGRectIsEmpty(self.toFrame)) || (!self.isDismissing && !CGRectIsEmpty(self.fromFrame))) {
  40. imageView = [[UIImageView alloc] initWithImage:self.image];
  41. imageView.frame = self.fromFrame;
  42. [containerView addSubview:imageView];
  43. }
  44. if (self.isDismissing == NO) {
  45. [containerView addSubview:cropViewController.view];
  46. [containerView bringSubviewToFront:imageView];
  47. }
  48. else {
  49. [containerView insertSubview:previousController.view belowSubview:cropViewController.view];
  50. }
  51. if (self.prepareForTransitionHandler)
  52. self.prepareForTransitionHandler();
  53. cropViewController.view.alpha = (self.isDismissing ? 1.0f : 0.0f);
  54. if (imageView) {
  55. [UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0.0f usingSpringWithDamping:1.0f initialSpringVelocity:0.7f options:0 animations:^{
  56. imageView.frame = self.toFrame;
  57. } completion:^(BOOL complete) {
  58. [imageView removeFromSuperview];
  59. }];
  60. }
  61. [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
  62. cropViewController.view.alpha = (self.isDismissing ? 0.0f : 1.0f);
  63. } completion:^(BOOL complete) {
  64. [self reset];
  65. [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
  66. }];
  67. }
  68. - (void)reset
  69. {
  70. self.image = nil;
  71. self.fromFrame = CGRectZero;
  72. self.toFrame = CGRectZero;
  73. self.prepareForTransitionHandler = nil;
  74. }
  75. @end