This repository was archived by the owner on Dec 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathPFColorLitePreviewView.m
More file actions
executable file
·96 lines (76 loc) · 3.09 KB
/
PFColorLitePreviewView.m
File metadata and controls
executable file
·96 lines (76 loc) · 3.09 KB
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
90
91
92
93
94
95
96
#import "PFColorLitePreviewView.h"
#import <CoreGraphics/CoreGraphics.h>
@implementation PFColorLitePreviewView {
UIColor *_tintColor;
}
- (void)updateWithColor:(UIColor *)color {
self.mainColor = color;
[self setNeedsDisplay];
}
- (void)setMainColor:(UIColor *)mainColor
previousColor:(UIColor *)prevColor {
self.mainColor = mainColor;
if (prevColor)
self.previousColor = prevColor;
}
- (id)initWithFrame:(CGRect)frame
tintColor:(UIColor *)tintColor
mainColor:(UIColor *)mainColor
previousColor:(UIColor *)prevColor {
self = [super initWithFrame:frame];
if (self) {
self.mainColor = mainColor;
_tintColor = tintColor;
if (prevColor)
self.previousColor = prevColor;
[self setBackgroundColor:[UIColor clearColor]];
}
return self;
}
- (void)drawRect:(CGRect)rect {
if (!self.mainColor)
self.mainColor = [UIColor whiteColor];
float halfWidth = rect.size.width / 2;
float halfHeight = rect.size.height / 2;
float oneThirdWidth = rect.size.width / 3;
float twoPi = M_PI * 2;
float threePi = M_PI * 3;
float halfPi = M_PI / 2;
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextScaleCTM(context, 1, 1);
CGContextSetLineWidth(context, halfWidth / 5);
CGFloat red, green, blue;
[_tintColor getRed:&red green:&green blue:&blue alpha:nil];
CGContextSetRGBStrokeColor(context, red, green, blue, 0.3f);
CGContextAddArc(context, halfWidth, halfHeight, oneThirdWidth, 0, twoPi, 1);
CGContextDrawPath(context, kCGPathStroke);
CGContextAddArc(context, halfWidth, halfHeight, oneThirdWidth, 0, twoPi, 1);
UIGraphicsBeginImageContextWithOptions(rect.size, NO, [UIScreen mainScreen].scale);
int kHeight = 12;
int kWidth = 12;
NSArray *colors = @[UIColor.whiteColor, UIColor.grayColor];
for (int row = 0; row < rect.size.height; row += kHeight) {
int index = row % (kHeight * 2) == 0 ? 0 : 1;
for (int col = 0; col < rect.size.width; col += kWidth) {
[colors[index++ % 2] setFill];
UIRectFill(CGRectMake(col, row, kWidth, kHeight));
}
}
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGContextSetFillColorWithColor(context, [UIColor colorWithPatternImage:img].CGColor);
CGContextDrawPath(context, kCGPathEOFill);
if (self.previousColor) {
CGContextAddArc(context, halfWidth, halfHeight, oneThirdWidth, threePi + halfPi, halfPi, 1);
CGContextSetFillColorWithColor(context, self.mainColor.CGColor);
CGContextDrawPath(context, kCGPathEOFill);
CGContextAddArc(context, halfWidth, halfHeight, oneThirdWidth, halfPi, threePi / 2, 0);
CGContextSetFillColorWithColor(context, self.previousColor.CGColor);
CGContextDrawPath(context, kCGPathEOFill);
} else {
CGContextAddArc(context, halfWidth, halfHeight, oneThirdWidth, 0, twoPi, 1);
CGContextSetFillColorWithColor(context, self.mainColor.CGColor);
CGContextDrawPath(context, kCGPathEOFill);
}
}
@end