Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions DYRateView.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
objects = {

/* Begin PBXBuildFile section */
0886168C1A00ECC200E0F0EE /* StarHalf.png in Resources */ = {isa = PBXBuildFile; fileRef = 088616881A00ECC200E0F0EE /* StarHalf.png */; };
0886168D1A00ECC200E0F0EE /* StarHalf@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 088616891A00ECC200E0F0EE /* StarHalf@2x.png */; };
0886168E1A00ECC200E0F0EE /* StarHalfLarge.png in Resources */ = {isa = PBXBuildFile; fileRef = 0886168A1A00ECC200E0F0EE /* StarHalfLarge.png */; };
0886168F1A00ECC200E0F0EE /* StarHalfLarge@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 0886168B1A00ECC200E0F0EE /* StarHalfLarge@2x.png */; };
D353DC55F2240468AE06EA5E /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D353DC55F2240468AE06EA5D /* UIKit.framework */; };
D353DC55F2240468AE06EA60 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D353DC55F2240468AE06EA5F /* Foundation.framework */; };
D353DC55F2240468AE06EA62 /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D353DC55F2240468AE06EA61 /* CoreGraphics.framework */; };
Expand All @@ -27,6 +31,10 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
088616881A00ECC200E0F0EE /* StarHalf.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = StarHalf.png; sourceTree = "<group>"; };
088616891A00ECC200E0F0EE /* StarHalf@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "StarHalf@2x.png"; sourceTree = "<group>"; };
0886168A1A00ECC200E0F0EE /* StarHalfLarge.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = StarHalfLarge.png; sourceTree = "<group>"; };
0886168B1A00ECC200E0F0EE /* StarHalfLarge@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "StarHalfLarge@2x.png"; sourceTree = "<group>"; };
D353DC55F2240468AE06EA54 /* DYRateView.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = DYRateView.app; sourceTree = BUILT_PRODUCTS_DIR; };
D353DC55F2240468AE06EA5D /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; };
D353DC55F2240468AE06EA5F /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
Expand Down Expand Up @@ -129,6 +137,10 @@
D353DC55F2240468AE06EA79 /* Resources */ = {
isa = PBXGroup;
children = (
088616881A00ECC200E0F0EE /* StarHalf.png */,
088616891A00ECC200E0F0EE /* StarHalf@2x.png */,
0886168A1A00ECC200E0F0EE /* StarHalfLarge.png */,
0886168B1A00ECC200E0F0EE /* StarHalfLarge@2x.png */,
D353DC55F2240468AE06EA88 /* StarEmpty.png */,
D353DC55F2240468AE06EA86 /* StarEmpty@2x.png */,
D353DC55F2240468AE06EA84 /* StarEmptyLarge.png */,
Expand Down Expand Up @@ -166,6 +178,8 @@
/* Begin PBXProject section */
D353DC55F2240468AE06EA4E /* Project object */ = {
isa = PBXProject;
attributes = {
};
buildConfigurationList = D353DC55F2240468AE06EA4F /* Build configuration list for PBXProject "DYRateView" */;
compatibilityVersion = "Xcode 3.2";
developmentRegion = English;
Expand All @@ -189,15 +203,19 @@
buildActionMask = 2147483647;
files = (
D353DC55F2240468AE06EA68 /* InfoPlist.strings in Resources */,
0886168D1A00ECC200E0F0EE /* StarHalf@2x.png in Resources */,
D353DC55F2240468AE06EA74 /* (null) in Resources */,
D353DC55F2240468AE06EA7B /* StarFullLarge@2x.png in Resources */,
D353DC55F2240468AE06EA7D /* StarFullLarge.png in Resources */,
D353DC55F2240468AE06EA7F /* StarFull@2x.png in Resources */,
0886168C1A00ECC200E0F0EE /* StarHalf.png in Resources */,
D353DC55F2240468AE06EA81 /* StarFull.png in Resources */,
D353DC55F2240468AE06EA83 /* StarEmptyLarge@2x.png in Resources */,
D353DC55F2240468AE06EA85 /* StarEmptyLarge.png in Resources */,
D353DC55F2240468AE06EA87 /* StarEmpty@2x.png in Resources */,
D353DC55F2240468AE06EA89 /* StarEmpty.png in Resources */,
0886168E1A00ECC200E0F0EE /* StarHalfLarge.png in Resources */,
0886168F1A00ECC200E0F0EE /* StarHalfLarge@2x.png in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
7 changes: 4 additions & 3 deletions DYRateView/DYRateView/DYRateView.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ typedef enum {
RateViewAlignmentRight
} RateViewAlignment;


@interface DYRateView : UIView {
UIImage *_fullStarImage;
UIImage *_emptyStarImage;
UIImage *_halfStarImage;
CGPoint _origin;
NSInteger _numOfStars;
CGFloat _numOfStars;
}

@property(nonatomic, assign) RateViewAlignment alignment;
Expand All @@ -46,10 +46,11 @@ typedef enum {
@property(nonatomic, assign) BOOL editable;
@property(nonatomic, retain) UIImage *fullStarImage;
@property(nonatomic, retain) UIImage *emptyStarImage;
@property(nonatomic, retain) UIImage *halfStarImage;
@property(nonatomic, assign) NSObject<DYRateViewDelegate> *delegate;

- (DYRateView *)initWithFrame:(CGRect)frame;
- (DYRateView *)initWithFrame:(CGRect)rect fullStar:(UIImage *)fullStarImage emptyStar:(UIImage *)emptyStarImage;
- (DYRateView *)initWithFrame:(CGRect)rect fullStar:(UIImage *)fullStarImage emptyStar:(UIImage *)emptyStarImage halfStar:(UIImage *)halfStarImage;

@end

Expand Down
33 changes: 25 additions & 8 deletions DYRateView/DYRateView/DYRateView.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE

static NSString *DefaultFullStarImageFilename = @"StarFull.png";
static NSString *DefaultEmptyStarImageFilename = @"StarEmpty.png";
static NSString *DefaultHalfStarImageFilename = @"StarHalf.png";

@interface DYRateView ()

Expand All @@ -46,20 +47,22 @@ @implementation DYRateView
@synthesize editable = _editable;
@synthesize fullStarImage = _fullStarImage;
@synthesize emptyStarImage = _emptyStarImage;
@synthesize halfStarImage = _halfStarImage;
@synthesize delegate = _delegate;

- (DYRateView *)initWithFrame:(CGRect)frame {
return [self initWithFrame:frame fullStar:[UIImage imageNamed:DefaultFullStarImageFilename] emptyStar:[UIImage imageNamed:DefaultEmptyStarImageFilename]];
return [self initWithFrame:frame fullStar:[UIImage imageNamed:DefaultFullStarImageFilename] emptyStar:[UIImage imageNamed:DefaultEmptyStarImageFilename] halfStar:[UIImage imageNamed:DefaultHalfStarImageFilename]];
}

- (DYRateView *)initWithFrame:(CGRect)frame fullStar:(UIImage *)fullStarImage emptyStar:(UIImage *)emptyStarImage {
- (DYRateView *)initWithFrame:(CGRect)frame fullStar:(UIImage *)fullStarImage emptyStar:(UIImage *)emptyStarImage halfStar:(UIImage *)halfStarImage {
self = [super initWithFrame:frame];
if (self) {
self.opaque = NO;
self.backgroundColor = [UIColor clearColor];

_fullStarImage = [fullStarImage retain];
_emptyStarImage = [emptyStarImage retain];
_halfStarImage = [halfStarImage retain];

[self commonSetup];
}
Expand All @@ -71,6 +74,7 @@ - (id)initWithCoder:(NSCoder *)decoder {
if (self) {
_fullStarImage = [[UIImage imageNamed:DefaultFullStarImageFilename] retain];
_emptyStarImage = [[UIImage imageNamed:DefaultEmptyStarImageFilename] retain];
_halfStarImage = [[UIImage imageNamed:DefaultHalfStarImageFilename] retain];

[self commonSetup];
}
Expand All @@ -80,6 +84,7 @@ - (id)initWithCoder:(NSCoder *)decoder {
- (void)dealloc {
[_fullStarImage release]; _fullStarImage = nil;
[_emptyStarImage release]; _emptyStarImage = nil;
[_halfStarImage release]; _halfStarImage = nil;
[super dealloc];
}

Expand Down Expand Up @@ -119,12 +124,16 @@ - (void)drawRect:(CGRect)rect
x += _fullStarImage.size.width + _padding;
}


float floor = floorf(_rate);
x = _origin.x;
for (int i = 0; i < floor; i++) {
[_fullStarImage drawAtPoint:CGPointMake(x, _origin.y)];
x += _fullStarImage.size.width + _padding;
if (_rate - (CGFloat)i == 0.5) {
[_halfStarImage drawAtPoint:CGPointMake(x, _origin.y)];
x += _halfStarImage.size.width + _padding;
} else {
[_fullStarImage drawAtPoint:CGPointMake(x, _origin.y)];
x += _fullStarImage.size.width + _padding;
}
}

if (_numOfStars - floor > 0.01) {
Expand Down Expand Up @@ -168,10 +177,18 @@ - (void)setEmptyStarImage:(UIImage *)emptyStarImage
}
}

- (void)setHalfStarImage:(UIImage *)halfStarImage {
if (halfStarImage != _halfStarImage) {
[_halfStarImage release];
_halfStarImage = [halfStarImage retain];
[self setNeedsDisplay];
}
}

- (void)handleTouchAtLocation:(CGPoint)location {
for(int i = _numOfStars - 1; i > -1; i--) {
if (location.x > _origin.x + i * (_fullStarImage.size.width + _padding) - _padding / 2.) {
self.rate = i + 1;
for(float i = _numOfStars*2 - 1; i > -1; i--) {
if (location.x > _origin.x + i/2 * (_fullStarImage.size.width + _padding) - _padding / 2.) {
self.rate = i/2.0 + 0.5;
return;
}
}
Expand Down
4 changes: 2 additions & 2 deletions DYRateView/DemoViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ - (void)viewDidLoad
}

- (void)setUpEditableRateView {
DYRateView *rateView = [[DYRateView alloc] initWithFrame:CGRectMake(0, 40, self.view.bounds.size.width, 20) fullStar:[UIImage imageNamed:@"StarFullLarge.png"] emptyStar:[UIImage imageNamed:@"StarEmptyLarge.png"]];
DYRateView *rateView = [[DYRateView alloc] initWithFrame:CGRectMake(0, 40, self.view.bounds.size.width, 20) fullStar:[UIImage imageNamed:@"StarFullLarge.png"] emptyStar:[UIImage imageNamed:@"StarEmptyLarge.png"] halfStar:[UIImage imageNamed:@"StarHalfLarge.png"]];
rateView.padding = 20;
rateView.alignment = RateViewAlignmentCenter;
rateView.editable = YES;
Expand Down Expand Up @@ -113,7 +113,7 @@ - (void)dealloc {
#pragma mark - DYRateViewDelegate

- (void)rateView:(DYRateView *)rateView changedToNewRate:(NSNumber *)rate {
self.rateLabel.text = [NSString stringWithFormat:@"Rate: %d", rate.intValue];
self.rateLabel.text = [NSString stringWithFormat:@"Rate: %.1f", rate.floatValue];
}

@end
Binary file added DYRateView/Resources/StarHalf.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added DYRateView/Resources/StarHalf@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added DYRateView/Resources/StarHalfLarge.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added DYRateView/Resources/StarHalfLarge@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.