-
Notifications
You must be signed in to change notification settings - Fork 4
/
UIView+ShrinkTo.m
89 lines (71 loc) · 3.38 KB
/
UIView+ShrinkTo.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//
// UIView+ShrinkTo.m
// ShrinkToExample
//
// Created by Ash Furrow on 12-02-18.
// Copyright (c) 2012 500px. All rights reserved.
//
#import <objc/runtime.h>
#import <QuartzCore/QuartzCore.h>
#import "UIView+ShrinkTo.h"
@implementation UIView (ShrinkTo)
static NSString *associativeKey = @"associated NSImage";
-(void)shrinkToCenterOfView:(UIView *)targetView
{
[self shrinkToPoint:targetView.center inView:targetView];
}
-(void)shrinkToPoint:(CGPoint)thePoint inView:(UIView *)theTargetView
{
UIView *sharedSuperView = self.superview;
while (![theTargetView isDescendantOfView:sharedSuperView])
{
sharedSuperView = [sharedSuperView superview];
if (sharedSuperView == nil)
{
NSLog(@"We couldn't find a common ancester view to re-add the cell to.");
return;
}
}
CGRect newFrame = [self.superview convertRect:self.frame toView:sharedSuperView];
CGPoint targetPointInSharedSuperview = [sharedSuperView convertPoint:thePoint fromView:theTargetView];
UIImage *viewImage;
UIGraphicsBeginImageContext(self.bounds.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *imageView = [[[UIImageView alloc] initWithImage:viewImage] autorelease];
imageView.frame = newFrame;
[sharedSuperView addSubview:imageView];
//These animations scale the view down, reduce it's opacity, and move it along a bezier curve
CABasicAnimation *scaleAnim = [CABasicAnimation animationWithKeyPath:@"transform"];
scaleAnim.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
scaleAnim.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)];
scaleAnim.removedOnCompletion = NO;
scaleAnim.duration = SHRINK_ANIMATION_DURATION;
CABasicAnimation *opacityAnim = [CABasicAnimation animationWithKeyPath:@"alpha"];
opacityAnim.fromValue = [NSNumber numberWithFloat:1.0];
opacityAnim.toValue = [NSNumber numberWithFloat:0.1];
opacityAnim.removedOnCompletion = NO;
opacityAnim.duration = SHRINK_ANIMATION_DURATION;
UIBezierPath *movePath = [UIBezierPath bezierPath];
CGPoint ctlPoint = CGPointMake(imageView.center.x, targetPointInSharedSuperview.y);
[movePath moveToPoint:imageView.center];
[movePath addQuadCurveToPoint:targetPointInSharedSuperview controlPoint:ctlPoint];
CAKeyframeAnimation *moveAnim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
moveAnim.path = movePath.CGPath;
moveAnim.removedOnCompletion = NO;
CAAnimationGroup *animGroup = [CAAnimationGroup animation];
animGroup.animations = [NSArray arrayWithObjects:moveAnim, scaleAnim, opacityAnim, nil];
animGroup.delegate = self;
[imageView.layer addAnimation:animGroup forKey:nil];
objc_setAssociatedObject(self, &associativeKey, imageView, OBJC_ASSOCIATION_RETAIN);
}
//after we're done animating, we need to remove ourselves from the view so we don't "hang around" on the screen
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
if (!flag) return;
UIImageView *imageView = (UIImageView *)objc_getAssociatedObject(self, &associativeKey);
[imageView removeFromSuperview];
objc_setAssociatedObject(self, &associativeKey, nil, OBJC_ASSOCIATION_ASSIGN);
}
@end