/
OCSelectionView.m
201 lines (161 loc) · 6.09 KB
/
OCSelectionView.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//
// OCSelectionView.m
// OCCalendar
//
// Created by Oliver Rickard on 3/30/12.
// Copyright (c) 2012 UC Berkeley. All rights reserved.
//
#import "OCSelectionView.h"
@implementation OCSelectionView
@synthesize selectionMode = _selectionMode;
@synthesize selected;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
selected = NO;
startCellX = -1;
startCellY = -1;
endCellX = -1;
endCellY = -1;
hDiff = UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone ? 41 : 43;
vDiff = 30;
self.userInteractionEnabled = YES;
self.backgroundColor = [UIColor clearColor];
}
return self;
}
- (BOOL) selected {
return selected;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
if(selected) {
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = UIGraphicsGetCurrentContext();
CGColorRef backgroundShadow = [UIColor blackColor].CGColor;
CGSize backgroundShadowOffset = CGSizeMake(2, 3);
CGFloat backgroundShadowBlurRadius = 5;
UIColor* darkColor = [UIColor colorWithRed: 0 green: 0 blue: 0 alpha: 0.72];
UIColor* color = [UIColor colorWithRed: 0.82 green: 0.08 blue: 0 alpha: 0.86];
UIColor* color2 = [UIColor colorWithRed: 0.66 green: 0.02 blue: 0.04 alpha: 0.88];
NSArray* gradient3Colors = [NSArray arrayWithObjects:
(id)color.CGColor,
(id)color2.CGColor, nil];
CGFloat gradient3Locations[] = {0, 1};
CGGradientRef gradient3 = CGGradientCreateWithColors(colorSpace, (CFArrayRef)gradient3Colors, gradient3Locations);
int tempStart = MIN(startCellY, endCellY);
int tempEnd = MAX(startCellY, endCellY);
for(int i = tempStart; i <= tempEnd; i++) {
//// selectedRect Drawing
int thisRowEndCell;
int thisRowStartCell;
if(startCellY == i) {
thisRowStartCell = startCellX;
if (startCellY > endCellY) {
thisRowStartCell = 0; thisRowEndCell = startCellX;
}
} else {
thisRowStartCell = 0;
}
if(endCellY == i) {
thisRowEndCell = endCellX;
if (startCellY > endCellY) {
thisRowStartCell = endCellX; thisRowEndCell = 6;
}
} else if (!(startCellY > endCellY)) {
thisRowEndCell = 6;
}
CGFloat cornerRadius;
if(_selectionMode == OCSelectionSingleDate) {
cornerRadius = 0.0;
} else {
cornerRadius = 10.0;
}
//// selectedRect Drawing
float width_offset= UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone ? 25 : 20; //device specific width offset i.e., iPhone vs iPad
UIBezierPath* selectedRectPath = [UIBezierPath bezierPathWithRoundedRect: CGRectMake(MIN(thisRowStartCell, thisRowEndCell)*hDiff, i*vDiff, (ABS(thisRowEndCell-thisRowStartCell))*hDiff+width_offset, 21) cornerRadius: cornerRadius];
CGContextSaveGState(context);
[selectedRectPath addClip];
CGContextDrawLinearGradient(context, gradient3, CGPointMake((MIN(thisRowStartCell, thisRowEndCell)+.5)*hDiff, (i+1)*vDiff), CGPointMake((MIN(thisRowStartCell, thisRowEndCell)+.5)*hDiff, i*vDiff), 0);
CGContextRestoreGState(context);
CGContextSaveGState(context);
CGContextSetShadowWithColor(context, backgroundShadowOffset, backgroundShadowBlurRadius, backgroundShadow);
[darkColor setStroke];
selectedRectPath.lineWidth = 0.5;
[selectedRectPath stroke];
CGContextRestoreGState(context);
}
CGGradientRelease(gradient3);
CGColorSpaceRelease(colorSpace);
}
}
-(void) singleSelection:(NSSet *)touches {
self.selected = YES;
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
startCellX = MIN((int)(point.x)/hDiff,6);
startCellY = (int)(point.y)/vDiff;
endCellX = MIN(startCellX,6);
endCellY = startCellY;
[self setNeedsDisplay];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self singleSelection:touches];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if(_selectionMode == OCSelectionSingleDate) {
[self singleSelection:touches];
return;
}
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
if(CGRectContainsPoint(self.bounds, point)) {
endCellX = MIN((int)(point.x)/hDiff,6);
endCellY = (int)(point.y)/vDiff;
[self setNeedsDisplay];
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if(_selectionMode == OCSelectionSingleDate) {
[self singleSelection:touches];
return;
}
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
if(CGRectContainsPoint(self.bounds, point)) {
endCellX = MIN((int)(point.x)/hDiff,6);
endCellY = (int)(point.y)/vDiff;
[self setNeedsDisplay];
}
}
-(void)resetSelection {
startCellX = -1;
startCellY = -1;
endCellY = -1;
endCellX = -1;
selected = NO;
[self setNeedsDisplay];
}
-(CGPoint)startPoint {
return CGPointMake(startCellX, startCellY);
}
-(CGPoint)endPoint {
return CGPointMake(endCellX, endCellY);
}
-(void)setStartPoint:(CGPoint)sPoint {
startCellX = sPoint.x;
startCellY = sPoint.y;
selected = YES;
[self setNeedsDisplay];
}
-(void)setEndPoint:(CGPoint)ePoint {
endCellX = ePoint.x;
endCellY = ePoint.y;
selected = YES;
[self setNeedsDisplay];
}
@end