Today's Menu  Portugal
journal and plan nutrition
DLStarRatingControl.m
Go to the documentation of this file.
1 /*
2 
3  DLStarRating
4  Copyright (C) 2011 David Linsin <dlinsin@gmail.com>
5 
6  All rights reserved. This program and the accompanying materials
7  are made available under the terms of the Eclipse Public License v1.0
8  which accompanies this distribution, and is available at
9  http://www.eclipse.org/legal/epl-v10.html
10 
11  */
12 
13 #import "DLStarRatingControl.h"
14 #import "DLStarView.h"
15 #import "UIView+Subviews.h"
16 
17 
18 @implementation DLStarRatingControl
19 
21 
22 #pragma mark -
23 #pragma mark Initialization
24 
25 - (void)setupView {
26  self.clipsToBounds = YES;
27  currentIdx = -1;
28  star = [UIImage imageNamed:@"star.png"];
29  highlightedStar = [UIImage imageNamed:@"star_highlighted.png"];
30 
31  for (int i=0; i<numberOfStars; i++) {
32  DLStarView *v = [[DLStarView alloc] initWithDefault:self.star highlighted:self.highlightedStar position:i allowFractions:isFractionalRatingEnabled];
33  [self addSubview:v];
34  }
35 }
36 
37 - (id)initWithCoder:(NSCoder *)aDecoder {
38  self = [super initWithCoder:aDecoder];
39  if (self) {
43  [self setupView];
44  }
45  return self;
46 }
47 
48 - (id)initWithFrame:(CGRect)frame {
49  self = [super initWithFrame:frame];
50  if (self) {
54  [self setupView];
55 
56  }
57  return self;
58 }
59 
60 - (id)initWithFrame:(CGRect)frame andStars:(NSUInteger)_numberOfStars isFractional:(BOOL)isFract{
61  self = [super initWithFrame:frame];
62  if (self) {
63  isFractionalRatingEnabled = isFract;
64  numberOfStars = (int)_numberOfStars;
67  [self setupView];
68  }
69  return self;
70 }
71 
72 - (void)layoutSubviews {
73  for (int i=0; i < numberOfStars; i++) {
74  [(DLStarView*)[self subViewWithTag:i] centerIn:self.frame with:numberOfStars];
75  }
76 }
77 
78 #pragma mark -
79 #pragma mark Customization
80 
81 - (void)setStar:(UIImage*)defaultStarImage highlightedStar:(UIImage*)highlightedStarImage {
82  for(NSInteger i = 0; i < numberOfStars; i++){
83  [self setStar:defaultStarImage highlightedStar:highlightedStarImage atIndex:(int)i];
84  }
85 }
86 
87 - (void)setStar:(UIImage*)defaultStarImage highlightedStar:(UIImage*)highlightedStarImage atIndex:(int)index {
88  DLStarView *selectedStar = (DLStarView*)[self subViewWithTag:index];
89 
90  // check if star exists
91  if (!selectedStar) return;
92 
93  // check images for nil else use default stars
94  defaultStarImage = (defaultStarImage) ? defaultStarImage : star;
95  highlightedStarImage = (highlightedStarImage) ? highlightedStarImage : highlightedStar;
96 
97  [selectedStar setStarImage:defaultStarImage highlightedStarImage:highlightedStarImage];
98 }
99 
100 #pragma mark -
101 #pragma mark Touch Handling
102 
103 - (UIButton*)starForPoint:(CGPoint)point {
104  for (int i=0; i < numberOfStars; i++) {
105  if (CGRectContainsPoint([self subViewWithTag:i].frame, point)) {
106  return (UIButton*)[self subViewWithTag:i];
107  }
108  }
109  return nil;
110 }
111 
112 - (void)disableStarsDownToExclusive:(int)idx {
113  for (int i=numberOfStars; i > idx; --i) {
114  UIButton *b = (UIButton*)[self subViewWithTag:i];
115  b.highlighted = NO;
116  }
117 }
118 
119 - (void)disableStarsDownTo:(int)idx {
120  for (int i=numberOfStars; i >= idx; --i) {
121  UIButton *b = (UIButton*)[self subViewWithTag:i];
122  b.highlighted = NO;
123  }
124 }
125 
126 
127 - (void)enableStarsUpTo:(int)idx {
128  for (int i=0; i <= idx; i++) {
129  UIButton *b = (UIButton*)[self subViewWithTag:i];
130  b.highlighted = YES;
131  }
132 }
133 
134 - (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
135  CGPoint point = [touch locationInView:self];
136  UIButton *pressedButton = [self starForPoint:point];
137  if (pressedButton) {
138  int idx = (int)pressedButton.tag;
139  if (pressedButton.highlighted) {
140  [self disableStarsDownToExclusive:idx];
141  } else {
142  [self enableStarsUpTo:idx];
143  }
144  currentIdx = idx;
145  }
146  return YES;
147 }
148 
149 - (void)cancelTrackingWithEvent:(UIEvent *)event {
150  [super cancelTrackingWithEvent:event];
151 }
152 
153 - (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
154  CGPoint point = [touch locationInView:self];
155 
156  UIButton *pressedButton = [self starForPoint:point];
157  if (pressedButton) {
158  int idx = (int)pressedButton.tag;
159  UIButton *currentButton = (UIButton*)[self subViewWithTag:currentIdx];
160 
161  if (idx < currentIdx) {
162  currentButton.highlighted = NO;
163  currentIdx = idx;
164  [self disableStarsDownToExclusive:idx];
165  } else if (idx > currentIdx) {
166  currentButton.highlighted = YES;
167  pressedButton.highlighted = YES;
168  currentIdx = idx;
169  [self enableStarsUpTo:idx];
170  }
171  } else if (point.x < [self subViewWithTag:0].frame.origin.x) {
172  ((UIButton*)[self subViewWithTag:0]).highlighted = NO;
173  currentIdx = -1;
175  }
176  return YES;
177 }
178 
179 - (void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
180  [self.delegate newRating:self :self.rating];
181  [super endTrackingWithTouch:touch withEvent:event];
182 }
183 
184 #pragma mark -
185 #pragma mark Rating Property
186 
187 - (void)setRating:(float)_rating {
189  _rating *=kNumberOfFractions;
190  }
191  [self disableStarsDownTo:0];
192  currentIdx = (int)_rating-1;
193  [self enableStarsUpTo:currentIdx];
194 }
195 
196 - (float)rating {
198  return (float)(currentIdx+1)/kNumberOfFractions;
199  }
200  return (NSUInteger)currentIdx+1;
201 }
202 
203 
204 #pragma mark -
205 #pragma mark Memory Management
206 
207 - (void)dealloc {
208  self.star = nil;
209  self.highlightedStar = nil;
210  self.delegate = nil;
211  // [super dealloc];
212 }
213 
214 @end
UIButton * starForPoint:(CGPoint point)
void setStarImage:highlightedStarImage:(UIImage *starImage, [highlightedStarImage] UIImage *highlightedImage)
Definition: DLStarView.m:81
id< U7ParamEditControlDelegate > delegate
#define kNumberOfFractions
void disableStarsDownToExclusive:(int idx)
#define kDefaultNumberOfStars
void setStar:highlightedStar:atIndex:(UIImage *defaultStarImage, [highlightedStar] UIImage *highlightedStarImage, [atIndex] int index)
void disableStarsDownTo:(int idx)