-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHCSStarRatingView.m
More file actions
executable file
·364 lines (302 loc) · 12.9 KB
/
HCSStarRatingView.m
File metadata and controls
executable file
·364 lines (302 loc) · 12.9 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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// HCSStarRatingView.m
//
// Copyright (c) 2015 Hugo Sousa
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#import "HCSStarRatingView.h"
@interface HCSStarRatingView ()
@property (nonatomic, readonly) BOOL shouldUseImages;
@end
@implementation HCSStarRatingView {
CGFloat _minimumValue;
NSUInteger _maximumValue;
CGFloat _value;
}
@dynamic minimumValue;
@dynamic maximumValue;
@dynamic value;
@dynamic shouldUseImages;
#pragma mark - Initialization
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self _customInit];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self _customInit];
}
return self;
}
- (void)_customInit {
self.exclusiveTouch = YES;
_minimumValue = 0;
_maximumValue = 5;
_value = 0;
_spacing = 5.f;
}
- (void)setNeedsLayout {
[super setNeedsLayout];
[self setNeedsDisplay];
}
#pragma mark - Properties
- (UIColor *)backgroundColor {
if ([super backgroundColor]) {
return [super backgroundColor];
} else {
return self.isOpaque ? [UIColor whiteColor] : [UIColor clearColor];
};
}
- (CGFloat)minimumValue {
return MAX(_minimumValue, 0);
}
- (void)setMinimumValue:(CGFloat)minimumValue {
if (_minimumValue != minimumValue) {
_minimumValue = minimumValue;
[self setNeedsDisplay];
}
}
- (NSUInteger)maximumValue {
return MAX(_minimumValue, _maximumValue);
}
- (void)setMaximumValue:(NSUInteger)maximumValue {
if (_maximumValue != maximumValue) {
_maximumValue = maximumValue;
[self setNeedsDisplay];
[self invalidateIntrinsicContentSize];
}
}
- (CGFloat)value {
return MIN(MAX(_value, _minimumValue), _maximumValue);
}
- (void)setValue:(CGFloat)value {
if (_value != value && value >= _minimumValue && value <= _maximumValue) {
_value = value;
[self sendActionsForControlEvents:UIControlEventValueChanged];
[self setNeedsDisplay];
}
}
- (void)setSpacing:(CGFloat)spacing {
_spacing = MAX(spacing, 0);
[self setNeedsDisplay];
}
- (void)setAllowsHalfStars:(BOOL)allowsHalfStars {
if (_allowsHalfStars != allowsHalfStars) {
_allowsHalfStars = allowsHalfStars;
[self setNeedsDisplay];
}
}
- (void)setEmptyStarImage:(UIImage *)emptyStarImage {
if (_emptyStarImage != emptyStarImage) {
_emptyStarImage = emptyStarImage;
[self setNeedsDisplay];
}
}
- (void)setHalfStarImage:(UIImage *)halfStarImage {
if (_halfStarImage != halfStarImage) {
_halfStarImage = halfStarImage;
[self setNeedsDisplay];
}
}
- (void)setFilledStarImage:(UIImage *)filledStarImage {
if (_filledStarImage != filledStarImage) {
_filledStarImage = filledStarImage;
[self setNeedsDisplay];
}
}
- (BOOL)shouldUseImages {
return (self.emptyStarImage!=nil && self.filledStarImage!=nil);
}
#pragma mark - Image Drawing
- (void)_drawStarImageWithFrame:(CGRect)frame tintColor:(UIColor*)tintColor highlighted:(BOOL)highlighted {
UIImage *image = highlighted ? self.filledStarImage : self.emptyStarImage;
[self _drawImage:image frame:frame tintColor:tintColor];
}
- (void)_drawHalfStarImageWithFrame:(CGRect)frame tintColor:(UIColor *)tintColor {
UIImage *image = self.halfStarImage;
if (image == nil) {
// first draw star outline
[self _drawStarImageWithFrame:frame tintColor:tintColor highlighted:NO];
image = self.filledStarImage;
CGRect imageFrame = CGRectMake(0, 0, image.size.width * image.scale / 2.f, image.size.height * image.scale);
frame.size.width /= 2.f;
CGImageRef imageRef = CGImageCreateWithImageInRect(image.CGImage, imageFrame);
UIImage *halfImage = [UIImage imageWithCGImage:imageRef scale:image.scale orientation:image.imageOrientation];
image = [halfImage imageWithRenderingMode:image.renderingMode];
CGImageRelease(imageRef);
}
[self _drawImage:image frame:frame tintColor:tintColor];
}
- (void)_drawImage:(UIImage *)image frame:(CGRect)frame tintColor:(UIColor *)tintColor {
if (image.renderingMode == UIImageRenderingModeAlwaysTemplate) {
[tintColor setFill];
}
[image drawInRect:frame];
}
#pragma mark - Shape Drawing
- (void)_drawStarShapeWithFrame:(CGRect)frame tintColor:(UIColor*)tintColor highlighted:(BOOL)highlighted {
UIBezierPath* starShapePath = UIBezierPath.bezierPath;
[starShapePath moveToPoint: CGPointMake(CGRectGetMinX(frame) + 0.62723 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 0.37309 * CGRectGetHeight(frame))];
[starShapePath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 0.50000 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 0.02500 * CGRectGetHeight(frame))];
[starShapePath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 0.37292 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 0.37309 * CGRectGetHeight(frame))];
[starShapePath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 0.02500 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 0.39112 * CGRectGetHeight(frame))];
[starShapePath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 0.30504 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 0.62908 * CGRectGetHeight(frame))];
[starShapePath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 0.20642 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 0.97500 * CGRectGetHeight(frame))];
[starShapePath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 0.50000 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 0.78265 * CGRectGetHeight(frame))];
[starShapePath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 0.79358 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 0.97500 * CGRectGetHeight(frame))];
[starShapePath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 0.69501 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 0.62908 * CGRectGetHeight(frame))];
[starShapePath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 0.97500 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 0.39112 * CGRectGetHeight(frame))];
[starShapePath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 0.62723 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 0.37309 * CGRectGetHeight(frame))];
[starShapePath closePath];
starShapePath.miterLimit = 4;
if (highlighted) {
[tintColor setFill];
[starShapePath fill];
}
[tintColor setStroke];
starShapePath.lineWidth = 1;
[starShapePath stroke];
}
- (void)_drawHalfStarShapeWithFrame:(CGRect)frame tintColor:(UIColor *)tintColor {
// first draw star outline
[self _drawStarShapeWithFrame:frame tintColor:tintColor highlighted:NO];
UIBezierPath* starShapePath = UIBezierPath.bezierPath;
[starShapePath moveToPoint: CGPointMake(CGRectGetMinX(frame) + 0.50000 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 0.02500 * CGRectGetHeight(frame))];
[starShapePath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 0.37292 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 0.37309 * CGRectGetHeight(frame))];
[starShapePath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 0.02500 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 0.39112 * CGRectGetHeight(frame))];
[starShapePath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 0.30504 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 0.62908 * CGRectGetHeight(frame))];
[starShapePath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 0.20642 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 0.97500 * CGRectGetHeight(frame))];
[starShapePath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 0.50000 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 0.78265 * CGRectGetHeight(frame))];
[starShapePath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 0.50000 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 0.02500 * CGRectGetHeight(frame))];
[starShapePath closePath];
starShapePath.miterLimit = 4;
[tintColor setFill];
[starShapePath fill];
[tintColor setStroke];
starShapePath.lineWidth = 1;
[starShapePath stroke];
}
#pragma mark - Drawing
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, self.backgroundColor.CGColor);
CGContextFillRect(context, rect);
CGFloat availableWidth = rect.size.width - (_spacing * (_maximumValue - 1));
CGFloat cellWidth = (availableWidth / _maximumValue);
CGFloat starSide = (cellWidth <= rect.size.height) ? cellWidth : rect.size.height;
for (int idx = 0; idx < _maximumValue; idx++) {
CGPoint center = CGPointMake(cellWidth*idx + cellWidth/2 + _spacing*idx, rect.size.height/2);
CGRect frame = CGRectMake(center.x - starSide/2, center.y - starSide/2, starSide, starSide);
BOOL highlighted = (idx+1 <= ceilf(_value));
if (_allowsHalfStars && highlighted && (idx+1 > _value)) {
[self _drawHalfStarWithFrame:frame tintColor:self.tintColor];
} else {
[self _drawStarWithFrame:frame tintColor:self.tintColor highlighted:highlighted];
}
}
}
- (void)_drawStarWithFrame:(CGRect)frame tintColor:(UIColor *)tintColor highlighted:(BOOL)highlighted {
if (self.shouldUseImages) {
[self _drawStarImageWithFrame:frame tintColor:tintColor highlighted:highlighted];
} else {
[self _drawStarShapeWithFrame:frame tintColor:tintColor highlighted:highlighted];
}
}
- (void)_drawHalfStarWithFrame:(CGRect)frame tintColor:(UIColor *)tintColor {
if (self.shouldUseImages) {
[self _drawHalfStarImageWithFrame:frame tintColor:tintColor];
} else {
[self _drawHalfStarShapeWithFrame:frame tintColor:tintColor];
}
}
#pragma mark - Touches
- (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
[super beginTrackingWithTouch:touch withEvent:event];
if (![self isFirstResponder]) {
[self becomeFirstResponder];
}
[self _handleTouch:touch];
return YES;
}
- (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
[super continueTrackingWithTouch:touch withEvent:event];
[self _handleTouch:touch];
return YES;
}
- (void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
[super endTrackingWithTouch:touch withEvent:event];
if ([self isFirstResponder]) {
[self resignFirstResponder];
}
[self _handleTouch:touch];
}
- (void)cancelTrackingWithEvent:(UIEvent *)event {
[super cancelTrackingWithEvent:event];
if ([self isFirstResponder]) {
[self resignFirstResponder];
}
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
return !self.isUserInteractionEnabled;
}
- (void)_handleTouch:(UITouch *)touch {
CGFloat cellWidth = self.bounds.size.width / _maximumValue;
CGPoint location = [touch locationInView:self];
CGFloat value = location.x / cellWidth;
if (_allowsHalfStars && value+.5f < ceilf(value)) {
self.value = floor(value)+.5f;
} else {
self.value = ceilf(value);
}
}
#pragma mark - First responder
- (BOOL)canBecomeFirstResponder {
return YES;
}
#pragma mark - Intrinsic Content Size
- (CGSize)intrinsicContentSize {
CGFloat height = 44.f;
return CGSizeMake(_maximumValue * height + (_maximumValue-1) * _spacing, height);
}
#pragma mark - Accessibility
- (BOOL)isAccessibilityElement {
return YES;
}
- (NSString *)accessibilityLabel {
return [super accessibilityLabel] ?: NSLocalizedString(@"Rating", @"Accessibility label for star rating control.");
}
- (UIAccessibilityTraits)accessibilityTraits {
return ([super accessibilityTraits] | UIAccessibilityTraitAdjustable);
}
- (NSString *)accessibilityValue {
return [@(self.value) description];
}
- (BOOL)accessibilityActivate {
return YES;
}
- (void)accessibilityIncrement {
self.value += self.allowsHalfStars ? .5f : 1.f;
}
- (void)accessibilityDecrement {
self.value -= self.allowsHalfStars ? .5f : 1.f;
}
@end