Skip to content

Commit

Permalink
Renamed SSStarSelector to SSRatingPicker. Started implmenting changes
Browse files Browse the repository at this point in the history
  • Loading branch information
soffes committed Feb 3, 2011
1 parent 151ae30 commit 5d89c8c
Show file tree
Hide file tree
Showing 9 changed files with 196 additions and 130 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
//
// SCStarSelectorDemoViewController.h
// SCRatingPickerDemoViewController.h
// SSCatalog
//
// Created by Sam Soffes on 2/2/11.
// Copyright 2011 Sam Soffes. All rights reserved.
//

@interface SCStarSelectorDemoViewController : UIViewController {
@interface SCRatingPickerDemoViewController : UIViewController {

}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
//
// SCStarSelectorDemoViewController.m
// SCRatingPickerDemoViewController.m
// SSCatalog
//
// Created by Sam Soffes on 2/2/11.
// Copyright 2011 Sam Soffes. All rights reserved.
//

#import "SCStarSelectorDemoViewController.h"
#import <SSToolkit/SSStarsSelector.h>
#import "SCRatingPickerDemoViewController.h"
#import <SSToolkit/SSRatingPicker.h>

@implementation SCStarSelectorDemoViewController
@implementation SCRatingPickerDemoViewController

#pragma mark Class Methods

+ (NSString *)title {
return @"Star Selector";
return @"Rating Picker";
}


Expand All @@ -25,7 +25,7 @@ - (void)viewDidLoad {
self.title = [[self class] title];
self.view.backgroundColor = [UIColor whiteColor];

SSStarsSelector *selector = [[SSStarsSelector alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 48.0f)];
SSRatingPicker *selector = [[SSRatingPicker alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 48.0f)];
selector.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin;
[self.view addSubview:selector];
[selector release];
Expand Down
2 changes: 1 addition & 1 deletion SSCatalog/Classes/SCRootViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ - (void)viewDidLoad {
nil],
[NSDictionary dictionaryWithObjectsAndKeys:
[NSArray arrayWithObjects:
@"SCStarSelectorDemoViewController",
@"SCRatingPickerDemoViewController",
@"SCSwitchDemoViewController",
nil], kClassesKey,
@"Controls", kTitleKey,
Expand Down
12 changes: 6 additions & 6 deletions SSCatalog/SSCatalog.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
B27B1CD612248F9D00111EA2 /* Icon-57.png in Resources */ = {isa = PBXBuildFile; fileRef = B27B1CCC12248F9D00111EA2 /* Icon-57.png */; };
B27B1CD712248F9D00111EA2 /* Icon-114.png in Resources */ = {isa = PBXBuildFile; fileRef = B27B1CCD12248F9D00111EA2 /* Icon-114.png */; };
B27B1CD812248F9D00111EA2 /* iTunesArtwork in Resources */ = {isa = PBXBuildFile; fileRef = B27B1CCE12248F9D00111EA2 /* iTunesArtwork */; };
B282192112FA7B8800BAF3C6 /* SCStarSelectorDemoViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = B282192012FA7B8800BAF3C6 /* SCStarSelectorDemoViewController.m */; };
B282192112FA7B8800BAF3C6 /* SCRatingPickerDemoViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = B282192012FA7B8800BAF3C6 /* SCRatingPickerDemoViewController.m */; };
B2B3CEEF1296F78D001BAC94 /* SCSwitchDemoViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = B2B3CEEE1296F78D001BAC94 /* SCSwitchDemoViewController.m */; };
B2B3CF561296FA76001BAC94 /* SSToolkit.bundle in Resources */ = {isa = PBXBuildFile; fileRef = B2B3CF551296FA76001BAC94 /* SSToolkit.bundle */; };
B2E0323F121DC49700D02434 /* libSSToolkit.a in Frameworks */ = {isa = PBXBuildFile; fileRef = B24E9ECA121DC3CF0085F81E /* libSSToolkit.a */; };
Expand Down Expand Up @@ -92,8 +92,8 @@
B27B1CCD12248F9D00111EA2 /* Icon-114.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "Icon-114.png"; sourceTree = "<group>"; };
B27B1CCE12248F9D00111EA2 /* iTunesArtwork */ = {isa = PBXFileReference; lastKnownFileType = file; path = iTunesArtwork; sourceTree = "<group>"; };
B27B1CCF12248F9D00111EA2 /* SSCatalog-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "SSCatalog-Info.plist"; sourceTree = "<group>"; };
B282191F12FA7B8800BAF3C6 /* SCStarSelectorDemoViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SCStarSelectorDemoViewController.h; sourceTree = "<group>"; };
B282192012FA7B8800BAF3C6 /* SCStarSelectorDemoViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SCStarSelectorDemoViewController.m; sourceTree = "<group>"; };
B282191F12FA7B8800BAF3C6 /* SCRatingPickerDemoViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SCRatingPickerDemoViewController.h; sourceTree = "<group>"; };
B282192012FA7B8800BAF3C6 /* SCRatingPickerDemoViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SCRatingPickerDemoViewController.m; sourceTree = "<group>"; };
B2B3CEED1296F78D001BAC94 /* SCSwitchDemoViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SCSwitchDemoViewController.h; sourceTree = "<group>"; };
B2B3CEEE1296F78D001BAC94 /* SCSwitchDemoViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SCSwitchDemoViewController.m; sourceTree = "<group>"; };
B2B3CF551296FA76001BAC94 /* SSToolkit.bundle */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.plug-in"; name = SSToolkit.bundle; path = ../Resources/SSToolkit.bundle; sourceTree = SOURCE_ROOT; };
Expand Down Expand Up @@ -244,8 +244,8 @@
B2B3CEFF1296F88B001BAC94 /* Control Demos */ = {
isa = PBXGroup;
children = (
B282191F12FA7B8800BAF3C6 /* SCStarSelectorDemoViewController.h */,
B282192012FA7B8800BAF3C6 /* SCStarSelectorDemoViewController.m */,
B282191F12FA7B8800BAF3C6 /* SCRatingPickerDemoViewController.h */,
B282192012FA7B8800BAF3C6 /* SCRatingPickerDemoViewController.m */,
B2B3CEED1296F78D001BAC94 /* SCSwitchDemoViewController.h */,
B2B3CEEE1296F78D001BAC94 /* SCSwitchDemoViewController.m */,
);
Expand Down Expand Up @@ -355,7 +355,7 @@
B257303D1292524F001FC061 /* SCLoadingViewDemoViewController.m in Sources */,
B2B3CEEF1296F78D001BAC94 /* SCSwitchDemoViewController.m in Sources */,
E7FD242712F4FFC5006A6691 /* SCBadgeTableViewCellDemoViewController.m in Sources */,
B282192112FA7B8800BAF3C6 /* SCStarSelectorDemoViewController.m in Sources */,
B282192112FA7B8800BAF3C6 /* SCRatingPickerDemoViewController.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
16 changes: 8 additions & 8 deletions SSToolkit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@
B27B1A2312241D6F00111EA2 /* SSCollectionViewItem.m in Sources */ = {isa = PBXBuildFile; fileRef = B27B1A2112241D6F00111EA2 /* SSCollectionViewItem.m */; };
B28218BE12FA6AE100BAF3C6 /* SSToolkitDefines.h in Headers */ = {isa = PBXBuildFile; fileRef = B28218BC12FA6AE100BAF3C6 /* SSToolkitDefines.h */; };
B28218BF12FA6AE100BAF3C6 /* SSToolkitDefines.m in Sources */ = {isa = PBXBuildFile; fileRef = B28218BD12FA6AE100BAF3C6 /* SSToolkitDefines.m */; };
B28219DD12FA8EB500BAF3C6 /* SSStarsSelector.h in Headers */ = {isa = PBXBuildFile; fileRef = B28219DB12FA8EB500BAF3C6 /* SSStarsSelector.h */; };
B28219DE12FA8EB500BAF3C6 /* SSStarsSelector.m in Sources */ = {isa = PBXBuildFile; fileRef = B28219DC12FA8EB500BAF3C6 /* SSStarsSelector.m */; };
B28219DD12FA8EB500BAF3C6 /* SSRatingPicker.h in Headers */ = {isa = PBXBuildFile; fileRef = B28219DB12FA8EB500BAF3C6 /* SSRatingPicker.h */; };
B28219DE12FA8EB500BAF3C6 /* SSRatingPicker.m in Sources */ = {isa = PBXBuildFile; fileRef = B28219DC12FA8EB500BAF3C6 /* SSRatingPicker.m */; };
B2AAE65C1281B73E0068EE7F /* SSNavigationController.h in Headers */ = {isa = PBXBuildFile; fileRef = B2AAE65A1281B73E0068EE7F /* SSNavigationController.h */; };
B2AAE65D1281B73E0068EE7F /* SSNavigationController.m in Sources */ = {isa = PBXBuildFile; fileRef = B2AAE65B1281B73E0068EE7F /* SSNavigationController.m */; };
B2AE1134126F963300534CF2 /* UIApplication+SSToolkitAdditions.h in Headers */ = {isa = PBXBuildFile; fileRef = B2AE1132126F963300534CF2 /* UIApplication+SSToolkitAdditions.h */; };
Expand Down Expand Up @@ -194,8 +194,8 @@
B27B1A2112241D6F00111EA2 /* SSCollectionViewItem.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SSCollectionViewItem.m; sourceTree = "<group>"; };
B28218BC12FA6AE100BAF3C6 /* SSToolkitDefines.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SSToolkitDefines.h; sourceTree = "<group>"; };
B28218BD12FA6AE100BAF3C6 /* SSToolkitDefines.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SSToolkitDefines.m; sourceTree = "<group>"; };
B28219DB12FA8EB500BAF3C6 /* SSStarsSelector.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SSStarsSelector.h; sourceTree = "<group>"; };
B28219DC12FA8EB500BAF3C6 /* SSStarsSelector.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SSStarsSelector.m; sourceTree = "<group>"; };
B28219DB12FA8EB500BAF3C6 /* SSRatingPicker.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SSRatingPicker.h; sourceTree = "<group>"; };
B28219DC12FA8EB500BAF3C6 /* SSRatingPicker.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SSRatingPicker.m; sourceTree = "<group>"; };
B2AAE65A1281B73E0068EE7F /* SSNavigationController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SSNavigationController.h; sourceTree = "<group>"; };
B2AAE65B1281B73E0068EE7F /* SSNavigationController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SSNavigationController.m; sourceTree = "<group>"; };
B2AE1132126F963300534CF2 /* UIApplication+SSToolkitAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIApplication+SSToolkitAdditions.h"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -391,8 +391,8 @@
B24E9E2D121DC29A0085F81E /* SSTextField.m */,
B24E9E2E121DC29A0085F81E /* SSTextView.h */,
B24E9E2F121DC29A0085F81E /* SSTextView.m */,
B28219DB12FA8EB500BAF3C6 /* SSStarsSelector.h */,
B28219DC12FA8EB500BAF3C6 /* SSStarsSelector.m */,
B28219DB12FA8EB500BAF3C6 /* SSRatingPicker.h */,
B28219DC12FA8EB500BAF3C6 /* SSRatingPicker.m */,
B2B3CED61296F637001BAC94 /* SSSwitch.h */,
B2B3CED71296F637001BAC94 /* SSSwitch.m */,
);
Expand Down Expand Up @@ -470,7 +470,7 @@
B2EA9F5E12F4A5C900C929D6 /* SSBadgeTableViewCell.h in Headers */,
B2EA9F6212F4A5E100C929D6 /* SSBadgeView.h in Headers */,
B28218BE12FA6AE100BAF3C6 /* SSToolkitDefines.h in Headers */,
B28219DD12FA8EB500BAF3C6 /* SSStarsSelector.h in Headers */,
B28219DD12FA8EB500BAF3C6 /* SSRatingPicker.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -582,7 +582,7 @@
B2EA9F5F12F4A5C900C929D6 /* SSBadgeTableViewCell.m in Sources */,
B2EA9F6312F4A5E100C929D6 /* SSBadgeView.m in Sources */,
B28218BF12FA6AE100BAF3C6 /* SSToolkitDefines.m in Sources */,
B28219DE12FA8EB500BAF3C6 /* SSStarsSelector.m in Sources */,
B28219DE12FA8EB500BAF3C6 /* SSRatingPicker.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
4 changes: 2 additions & 2 deletions SSToolkit/SSStarsSelector.h → SSToolkit/SSRatingPicker.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// SSStarsSelector.h
// SSRatingPicker.h
// SSToolkit
//
// Created by Sam Soffes on 2/2/11.
Expand All @@ -8,7 +8,7 @@

#import "SSGradientView.h"

@interface SSStarsSelector : SSGradientView {
@interface SSRatingPicker : SSGradientView {

CGFloat _numberOfStars;
NSUInteger _totalNumberOfStars;
Expand Down
169 changes: 169 additions & 0 deletions SSToolkit/SSRatingPicker.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
//
// SSRatingPicker.m
// SSToolkit
//
// Created by Sam Soffes on 2/2/11.
// Copyright 2011 Sam Soffes. All rights reserved.
//

#import "SSRatingPicker.h"
#import "SSDrawingMacros.h"
#import "UIImage+SSToolkitAdditions.h"
#import "UIView+SSToolkitAdditions.h"

@implementation SSRatingPicker

@synthesize numberOfStars = _numberOfStars;
@synthesize totalNumberOfStars = _totalNumberOfStars;
@synthesize emptyStarImage = _emptyStarImage;
@synthesize filledStarImage = _filledStarImage;
@synthesize starSize = _starSize;
@synthesize starSpacing = _starSpacing;
@synthesize textLabel = _textLabel;
@synthesize enableHalfStars = _enableHalfStars;

#pragma mark NSObject

- (void)dealloc {
[_emptyStarImage release];
[_filledStarImage release];
[_textLabel release];
[super dealloc];
}


#pragma mark UIView

- (id)initWithFrame:(CGRect)rect {
if ((self = [super initWithFrame:rect])) {
self.clipsToBounds = YES;
self.topColor = [UIColor colorWithRed:0.878f green:0.890f blue:0.906f alpha:1.0f];
self.bottomColor = [UIColor colorWithRed:0.961f green:0.965f blue:0.973f alpha:1.0f];
self.bottomBorderColor = [UIColor colorWithRed:0.839f green:0.839f blue:0.839f alpha:1.0f];
self.hasTopBorder = NO;
self.hasBottomBorder = YES;
self.showsInsets = NO;

self.emptyStarImage = [UIImage imageNamed:@"gray-star.png" bundle:kSSToolkitBundleName];
self.filledStarImage = [UIImage imageNamed:@"orange-star.png" bundle:kSSToolkitBundleName];
self.starSize = CGSizeMake(21.0f, 36.0f);
self.starSpacing = 19.0f;
self.numberOfStars = 0.0f;
self.totalNumberOfStars = 5;
self.enableHalfStars = NO;

UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.textColor = [UIColor colorWithRed:0.612f green:0.620f blue:0.624f alpha:1.0f];
label.shadowColor = [UIColor whiteColor];
label.shadowOffset = CGSizeMake(0.0f, 1.0f);
label.backgroundColor = [UIColor clearColor];
label.text = @"Tap a Star to Rate";
label.font = [UIFont boldSystemFontOfSize:10.0f];
label.textAlignment = UITextAlignmentCenter;
self.textLabel = label;
[self addSubview:label];
[label release];
}
return self;
}


- (CGSize)sizeThatFits:(CGSize)size {
return CGSizeMake(_starSize.width * (CGFloat)_totalNumberOfStars, _starSize.height);
}


- (void)layoutSubviews {
CGSize size = self.frame.size;
_textLabel.frame = CGRectMake(0.0f, size.height - 15.0f, size.width, 12.0f);
}


- (void)drawRect:(CGRect)rect {
[super drawRect:rect];

CGFloat totalWidth = (_starSize.width * (CGFloat)_totalNumberOfStars) +
(_starSpacing * (CGFloat)(_totalNumberOfStars - 1));
CGPoint origin = CGPointMake(roundf((rect.size.width - totalWidth) / 2.0f), 10.0f); // TODO: don't hard code the 10

for (NSUInteger i = 0; i < _totalNumberOfStars; i++) {
// TODO: Implement half stars
UIImage *image = (roundf(_numberOfStars) >= i + 1) ? _filledStarImage : _emptyStarImage;

[image drawInRect:CGRectMake(origin.x + (_starSize.width + _starSpacing) * (CGFloat)i, origin.y,
_starSize.width, _starSize.height)];
}
}


- (void)willMoveToSuperview:(UIView *)newSuperview {
[super willMoveToSuperview:newSuperview];

if (newSuperview) {
[self addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew context:nil];
[self addObserver:self forKeyPath:@"numberOfStars" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
[self addObserver:self forKeyPath:@"totalNumberOfStars" options:NSKeyValueObservingOptionNew context:nil];
[self addObserver:self forKeyPath:@"emptyStarImage" options:NSKeyValueObservingOptionNew context:nil];
[self addObserver:self forKeyPath:@"filledStarImage" options:NSKeyValueObservingOptionNew context:nil];
[self addObserver:self forKeyPath:@"starSize" options:NSKeyValueObservingOptionNew context:nil];
[self addObserver:self forKeyPath:@"starSpacing" options:NSKeyValueObservingOptionNew context:nil];
[self addObserver:self forKeyPath:@"enableHalfStars" options:NSKeyValueObservingOptionNew context:nil];
} else {
[self removeObserver:self forKeyPath:@"frame"];
[self removeObserver:self forKeyPath:@"numberOfStars"];
[self removeObserver:self forKeyPath:@"totalNumberOfStars"];
[self removeObserver:self forKeyPath:@"emptyStarImage"];
[self removeObserver:self forKeyPath:@"filledStarImage"];
[self removeObserver:self forKeyPath:@"starSize"];
[self removeObserver:self forKeyPath:@"starSpacing"];
[self removeObserver:self forKeyPath:@"enableHalfStars"];
}
}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
self.numberOfStars = 3.0f;
}


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

}


- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

}


#pragma mark NSKeyValueObserving

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqual:@"frame"] || [keyPath isEqual:@"totalNumberOfStars"] || [keyPath isEqual:@"emptyStarImage"] ||
[keyPath isEqual:@"filledStarImage"] || [keyPath isEqual:@"starSize"] || [keyPath isEqual:@"starSpacing"] ||
[keyPath isEqual:@"enableHalfStars"]) {
[self setNeedsDisplay];
return;
}

if ([keyPath isEqual:@"numberOfStars"]) {
[self setNeedsDisplay];

CGFloat new = [[change valueForKey:@"new"] floatValue];
CGFloat old = [[change valueForKey:@"old"] floatValue];

// Animate in the text label if necessary
if ((new > 0 && old == 0) || (new == 0 && old > 0)) {
[UIView beginAnimations:@"fadeTextLabel" context:nil];
[UIView setAnimationDuration:0.15];
_textLabel.alpha = (new == 0.0f) ? 1.0f : 0.0f;
[UIView commitAnimations];
}

return;
}

[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}

@end
Loading

0 comments on commit 5d89c8c

Please sign in to comment.