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
//
// ASRatingView.m
// AppShike
//
// Created by bl0ck on 12/19/11.
// Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//
#import "ASStarRatingView.h"
@implementation ASStarRatingView
@synthesize notSelectedStar = _notSelectedStar;
@synthesize selectedStar = _selectedStar;
@synthesize halfSelectedStar = _halfSelectedStar;
@synthesize canEdit = _canEdit;
@synthesize maxRating = _maxRating;
@synthesize midMargin = _midrMargin;
@synthesize leftMargin = _leftMargin;
@synthesize rightMargin = _rightMargin;
@synthesize minStarSize = _minStarSize;
@synthesize rating = _rating;
@synthesize minAllowedRating = _minAllowedRating;
@synthesize maxAllowedRating = _maxAllowedRating;
- (void)refreshStars {
for(int i = 0; i < _starViews.count; ++i) {
UIImageView *imageView = [_starViews objectAtIndex:i];
if (_rating >= i+1) {
imageView.image = _selectedStar;
} else if (_rating > i) {
imageView.image = _halfSelectedStar;
} else {
imageView.image = _notSelectedStar;
}
}
}
- (void)setupView {
for(int i = 0; i < _maxRating; ++i) {
UIImageView *imageView = [[[UIImageView alloc] init] autorelease];
imageView.contentMode = UIViewContentModeScaleAspectFit;
[_starViews addObject:imageView];
[self addSubview:imageView];
}
[self refreshStars];
}
- (void)baseInit {
_notSelectedStar = [[UIImage imageNamed:@"not_selected_star"] retain];
_selectedStar = [[UIImage imageNamed:@"selected_star"] retain];
_halfSelectedStar = [[UIImage imageNamed:@"half_selected_star"] retain];
_starViews = [[NSMutableArray array] retain];
_maxRating = kDefaultMaxRating;
_midMargin = kDefaultMidMargin;
_leftMargin = kDefaultLeftMargin;
_rightMargin = kDefaultRightMargin;
_minStarSize = kDefaultMinStarSize;
_minAllowedRating = kDefaultMinAllowedRating;
_maxAllowedRating = kDefaultMaxAllowedRating;
_rating = _minAllowedRating;
_canEdit = YES;
[self setupView];
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self baseInit];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
if ((self = [super initWithCoder:aDecoder])) {
[self baseInit];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
NSLog(@"%f, %f, %f, %lu", self.frame.size.width, _leftMargin, _midMargin, (unsigned long)_starViews.count);
float desiredImageWidth = (self.frame.size.width - (_leftMargin*2) - (_midMargin*_starViews.count)) / _starViews.count;
float imageWidth = MAX(_minStarSize.width, desiredImageWidth);
float imageHeight = MAX(_minStarSize.height, self.frame.size.height);
for (int i = 0; i < _starViews.count; ++i) {
UIImageView *imageView = [_starViews objectAtIndex:i];
CGRect imageFrame = CGRectMake(_leftMargin + i*(_midMargin+imageWidth), 0, imageWidth, imageHeight);
imageView.frame = imageFrame;
}
}
- (void)setMaxRating:(int)maxRating {
if (_maxAllowedRating == _maxRating) {
_maxAllowedRating = maxRating;
}
_maxRating = maxRating;
// Remove old image views
for(int i = 0; i < _starViews.count; ++i) {
UIImageView *imageView = (UIImageView *) [_starViews objectAtIndex:i];
[imageView removeFromSuperview];
}
[_starViews removeAllObjects];
// Add new image views
[self setupView];
// Relayout and refresh
[self setNeedsLayout];
[self refreshStars];
}
- (void)setRating:(float)rating {
_rating = rating;
[self refreshStars];
}
#pragma mark - Touch Detection
- (void)handleTouchAtLocation:(CGPoint)touchLocation {
if (!_canEdit) return;
_rating = 0;
for(int i = _starViews.count - 1; i >= 0; i--) {
UIImageView *imageView = [_starViews objectAtIndex:i];
if (touchLocation.x > imageView.frame.origin.x) {
_rating = i+1;
break;
}
}
_rating = MAX(_minAllowedRating, _rating);
_rating = MIN(_maxAllowedRating, _rating);
[self refreshStars];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView:self];
[self handleTouchAtLocation:touchLocation];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView:self];
[self handleTouchAtLocation:touchLocation];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
}
@end