forked from glenyi/FloatRatingView
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FloatRatingView.swift
318 lines (264 loc) · 9.84 KB
/
FloatRatingView.swift
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
//
// FloatRatingView.swift
// Rating Demo
//
// Created by Glen Yi on 2014-09-05.
// Copyright (c) 2014 On The Pursuit. All rights reserved.
//
import UIKit
@objc public protocol FloatRatingViewDelegate {
/**
Returns the rating value when touch events end
*/
func floatRatingView(ratingView: FloatRatingView, didUpdate rating: Float)
/**
Returns the rating value as the user pans
*/
optional func floatRatingView(ratingView: FloatRatingView, isUpdating rating: Float)
}
/**
A simple rating view that can set whole, half or floating point ratings.
*/
@IBDesignable
public class FloatRatingView: UIView {
// MARK: Float Rating View properties
public weak var delegate: FloatRatingViewDelegate?
/**
Array of empty image views
*/
private var emptyImageViews: [UIImageView] = []
/**
Array of full image views
*/
private var fullImageViews: [UIImageView] = []
/**
Sets the empty image (e.g. a star outline)
*/
@IBInspectable public var emptyImage: UIImage? {
didSet {
// Update empty image views
for imageView in self.emptyImageViews {
imageView.image = emptyImage
}
self.refresh()
}
}
/**
Sets the full image that is overlayed on top of the empty image.
Should be same size and shape as the empty image.
*/
@IBInspectable public var fullImage: UIImage? {
didSet {
// Update full image views
for imageView in self.fullImageViews {
imageView.image = fullImage
}
self.refresh()
}
}
/**
Sets the empty and full image view content mode.
*/
var imageContentMode: UIViewContentMode = UIViewContentMode.ScaleAspectFit
/**
Minimum rating.
*/
@IBInspectable public var minRating: Int = 0 {
didSet {
// Update current rating if needed
if self.rating < Float(minRating) {
self.rating = Float(minRating)
self.refresh()
}
}
}
/**
Max rating value.
*/
@IBInspectable public var maxRating: Int = 5 {
didSet {
let needsRefresh = maxRating != oldValue
if needsRefresh {
self.removeImageViews()
self.initImageViews()
// Relayout and refresh
self.setNeedsLayout()
self.refresh()
}
}
}
/**
Minimum image size.
*/
@IBInspectable public var minImageSize: CGSize = CGSize(width: 5.0, height: 5.0)
/**
Set the current rating.
*/
@IBInspectable public var rating: Float = 0 {
didSet {
if rating != oldValue {
self.refresh()
}
}
}
/**
Sets whether or not the rating view can be changed by panning.
*/
@IBInspectable public var editable: Bool = true
/**
Ratings change by 0.5. Takes priority over floatRatings property.
*/
@IBInspectable public var halfRatings: Bool = false
/**
Ratings change by floating point values.
*/
@IBInspectable public var floatRatings: Bool = false
// MARK: Initializations
required override public init(frame: CGRect) {
super.init(frame: frame)
self.initImageViews()
}
required public init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.initImageViews()
}
// MARK: Refresh hides or shows full images
func refresh() {
for i in 0..<self.fullImageViews.count {
let imageView = self.fullImageViews[i]
if self.rating>=Float(i+1) {
imageView.layer.mask = nil
imageView.hidden = false
}
else if self.rating>Float(i) && self.rating<Float(i+1) {
// Set mask layer for full image
let maskLayer = CALayer()
maskLayer.frame = CGRectMake(0, 0, CGFloat(self.rating-Float(i))*imageView.frame.size.width, imageView.frame.size.height)
maskLayer.backgroundColor = UIColor.blackColor().CGColor
imageView.layer.mask = maskLayer
imageView.hidden = false
}
else {
imageView.layer.mask = nil;
imageView.hidden = true
}
}
}
// MARK: Layout helper classes
// Calculates the ideal ImageView size in a given CGSize
func sizeForImage(image: UIImage, inSize size:CGSize) -> CGSize {
let imageRatio = image.size.width / image.size.height
let viewRatio = size.width / size.height
if imageRatio < viewRatio {
let scale = size.height / image.size.height
let width = scale * image.size.width
return CGSizeMake(width, size.height)
}
else {
let scale = size.width / image.size.width
let height = scale * image.size.height
return CGSizeMake(size.width, height)
}
}
// Override to calculate ImageView frames
override public func layoutSubviews() {
super.layoutSubviews()
if let emptyImage = self.emptyImage {
let desiredImageWidth = self.frame.size.width / CGFloat(self.emptyImageViews.count)
let maxImageWidth = max(self.minImageSize.width, desiredImageWidth)
let maxImageHeight = max(self.minImageSize.height, self.frame.size.height)
let imageViewSize = self.sizeForImage(emptyImage, inSize: CGSizeMake(maxImageWidth, maxImageHeight))
let imageXOffset = (self.frame.size.width - (imageViewSize.width * CGFloat(self.emptyImageViews.count))) /
CGFloat((self.emptyImageViews.count - 1))
for i in 0..<self.maxRating {
let imageFrame = CGRectMake(i==0 ? 0:CGFloat(i)*(imageXOffset+imageViewSize.width), 0, imageViewSize.width, imageViewSize.height)
var imageView = self.emptyImageViews[i]
imageView.frame = imageFrame
imageView = self.fullImageViews[i]
imageView.frame = imageFrame
}
self.refresh()
}
}
func removeImageViews() {
// Remove old image views
for i in 0..<self.emptyImageViews.count {
var imageView = self.emptyImageViews[i]
imageView.removeFromSuperview()
imageView = self.fullImageViews[i]
imageView.removeFromSuperview()
}
self.emptyImageViews.removeAll(keepCapacity: false)
self.fullImageViews.removeAll(keepCapacity: false)
}
func initImageViews() {
if self.emptyImageViews.count != 0 {
return
}
// Add new image views
for i in 0..<self.maxRating {
let emptyImageView = UIImageView()
emptyImageView.contentMode = self.imageContentMode
emptyImageView.image = self.emptyImage
self.emptyImageViews.append(emptyImageView)
self.addSubview(emptyImageView)
let fullImageView = UIImageView()
fullImageView.contentMode = self.imageContentMode
fullImageView.image = self.fullImage
self.fullImageViews.append(fullImageView)
self.addSubview(fullImageView)
}
}
// MARK: Touch events
// Calculates new rating based on touch location in view
func handleTouchAtLocation(touchLocation: CGPoint) {
if !self.editable {
return
}
var newRating: Float = 0
for i in stride(from: (self.maxRating-1), through: 0, by: -1) {
let imageView = self.emptyImageViews[i]
if touchLocation.x > imageView.frame.origin.x {
// Find touch point in image view
let newLocation = imageView.convertPoint(touchLocation, fromView:self)
// Find decimal value for float or half rating
if imageView.pointInside(newLocation, withEvent: nil) && (self.floatRatings || self.halfRatings) {
let decimalNum = Float(newLocation.x / imageView.frame.size.width)
newRating = Float(i) + decimalNum
if self.halfRatings {
newRating = Float(i) + (decimalNum > 0.75 ? 1:(decimalNum > 0.25 ? 0.5:0))
}
}
// Whole rating
else {
newRating = Float(i) + 1.0
}
break
}
}
// Check min rating
self.rating = newRating < Float(self.minRating) ? Float(self.minRating):newRating
// Update delegate
if let delegate = self.delegate {
delegate.floatRatingView?(self, isUpdating: self.rating)
}
}
override public func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
if let touch = touches.first as? UITouch {
let touchLocation = touch.locationInView(self)
self.handleTouchAtLocation(touchLocation)
}
}
override public func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
if let touch = touches.first as? UITouch {
let touchLocation = touch.locationInView(self)
self.handleTouchAtLocation(touchLocation)
}
}
override public func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
// Update delegate
if let delegate = self.delegate {
delegate.floatRatingView(self, didUpdate: self.rating)
}
}
}