/
UIView+DTFoundation.m
73 lines (58 loc) · 1.8 KB
/
UIView+DTFoundation.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
//
// UIView+DTFoundation.m
// DTFoundation
//
// Created by Oliver Drobnik on 12/23/11.
// Copyright (c) 2011 Cocoanetics. All rights reserved.
//
#import "UIView+DTFoundation.h"
#import <QuartzCore/QuartzCore.h>
NSString *shadowContext = @"Shadow";
@implementation UIView (DTFoundation)
- (UIImage *)snapshotImage
{
UIGraphicsBeginImageContext(self.bounds.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
- (void)setRoundedCornersWithRadius:(CGFloat)radius width:(CGFloat)width color:(UIColor *)color
{
self.clipsToBounds = YES;
self.layer.cornerRadius = radius;
self.layer.borderWidth = width;
if (color)
{
self.layer.borderColor = color.CGColor;
}
}
- (void)addShadowWithColor:(UIColor *)color alpha:(CGFloat)alpha radius:(CGFloat)radius offset:(CGSize)offset
{
self.layer.shadowOpacity = alpha;
self.layer.shadowRadius = radius;
self.layer.shadowOffset = offset;
if (color)
{
self.layer.shadowColor = [color CGColor];
}
// cannot have masking
self.layer.masksToBounds = NO;
}
- (void)updateShadowPathToBounds:(CGRect)bounds withDuration:(NSTimeInterval)duration
{
CGPathRef oldPath = self.layer.shadowPath;
CGPathRef newPath = CGPathCreateWithRect(bounds, NULL);
if (oldPath && duration>0)
{
CABasicAnimation *theAnimation = [CABasicAnimation animationWithKeyPath:@"shadowPath"];
theAnimation.duration = duration;
theAnimation.fromValue = (__bridge id)oldPath;
theAnimation.toValue = (__bridge id)newPath;
theAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[self.layer addAnimation:theAnimation forKey:@"shadowPath"];
}
self.layer.shadowPath = newPath;
CGPathRelease(newPath);
}
@end