Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fractional rating support #7

Merged
merged 2 commits into from

3 participants

kuchmiyalex David Linsin Ilia Lobsanov
kuchmiyalex

For issue #6

David Linsin dlinsin was assigned
David Linsin
Owner

Thanks a lot! I'll check it out in the next couple of days.

Ilia Lobsanov

Looking forward to this one!

David Linsin dlinsin merged commit 30621a0 into from
David Linsin dlinsin referenced this pull request from a commit
David Linsin minor cleanup of #7
* removed warnings
f236c10
David Linsin dlinsin referenced this pull request
Closed

Adopt tests #8

David Linsin dlinsin referenced this pull request from a commit
David Linsin Merge branch 'master' into 4_ARC
* master:
  updated podspec
  Basic spec file, the version and tag/commit for the build will need to be updated.
  no not retain delegate
  Added description for fractional images #9
  adopted description
  dropping ios3 support
  minor cleanup of #7
  Unused method removed
  Fractional rating support
  added zucchini reference
  Added UI testing with zucchini
  Added accessibility labels for individual stars
  moved link to main repo
  Revert "moved sample screenshot to Downloads area"
  moved sample screenshot to Downloads area
  added ability to set custom images for single stars and updated the demo project with this setting
  added ability to set custom images for single stars and updated the demo project with this setting
  updated for xcode 4

Conflicts:
	DLStarRating/DLStarRatingControl.h
	DLStarRating/DLStarRatingControl.m
	DLStarRatingDemo/Classes/DLStarRatingDemoViewController.m
	DLStarRatingDemo/DLStarRatingDemo.xcodeproj/project.pbxproj
6aa5e51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 9, 2012
  1. kuchmiyalex

    Fractional rating support

    kuchmiyalex authored
  2. kuchmiyalex

    Unused method removed

    kuchmiyalex authored
This page is out of date. Refresh to see the latest.
9 DLStarRating/DLStarRatingControl.h 100644 → 100755
View
@@ -13,6 +13,7 @@
#import <UIKit/UIKit.h>
#define kDefaultNumberOfStars 5
+#define kNumberOfFractions 10
@protocol DLStarRatingDelegate;
@@ -22,21 +23,23 @@
UIImage *star;
UIImage *highlightedStar;
IBOutlet id<DLStarRatingDelegate> delegate;
+ BOOL isFractionalRatingEnabled;
}
- (id)initWithFrame:(CGRect)frame;
-- (id)initWithFrame:(CGRect)frame andStars:(NSUInteger)_numberOfStars;
+- (id)initWithFrame:(CGRect)frame andStars:(NSUInteger)_numberOfStars isFractional:(BOOL)isFract;
- (void)setStar:(UIImage*)defaultStarImage highlightedStar:(UIImage*)highlightedStarImage atIndex:(int)index;
@property (retain,nonatomic) UIImage *star;
@property (retain,nonatomic) UIImage *highlightedStar;
-@property (nonatomic) NSUInteger rating;
+@property (nonatomic) float rating;
@property (retain,nonatomic) id<DLStarRatingDelegate> delegate;
+@property (nonatomic,assign) BOOL isFractionalRatingEnabled;
@end
@protocol DLStarRatingDelegate
-- (void)newRating:(DLStarRatingControl *)control: (NSUInteger)rating;
+-(void)newRating:(DLStarRatingControl *)control :(float)rating;
@end
32 DLStarRating/DLStarRatingControl.m 100644 → 100755
View
@@ -17,7 +17,7 @@
@implementation DLStarRatingControl
-@synthesize star, highlightedStar, delegate;
+@synthesize star, highlightedStar, delegate, isFractionalRatingEnabled;
#pragma mark -
#pragma mark Initialization
@@ -25,10 +25,11 @@ @implementation DLStarRatingControl
- (void)setupView {
self.clipsToBounds = YES;
currentIdx = -1;
- star = [[UIImage imageNamed:@"star.png"] retain];
- highlightedStar = [[UIImage imageNamed:@"star_highlighted.png"] retain];
+ star = [[UIImage imageNamed:@"star.png"] retain];
+ highlightedStar = [[UIImage imageNamed:@"star_highlighted.png"] retain];
+
for (int i=0; i<numberOfStars; i++) {
- DLStarView *v = [[DLStarView alloc] initWithDefault:self.star highlighted:self.highlightedStar position:i];
+ DLStarView *v = [[DLStarView alloc] initWithDefault:self.star highlighted:self.highlightedStar position:i allowFractions:isFractionalRatingEnabled];
[self addSubview:v];
[v release];
}
@@ -38,6 +39,8 @@ - (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
numberOfStars = kDefaultNumberOfStars;
+ if (isFractionalRatingEnabled)
+ numberOfStars *=kNumberOfFractions;
[self setupView];
}
return self;
@@ -47,15 +50,21 @@ - (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
numberOfStars = kDefaultNumberOfStars;
- [self setupView];
+ if (isFractionalRatingEnabled)
+ numberOfStars *=kNumberOfFractions;
+ [self setupView];
+
}
return self;
}
-- (id)initWithFrame:(CGRect)frame andStars:(NSUInteger)_numberOfStars {
+- (id)initWithFrame:(CGRect)frame andStars:(NSUInteger)_numberOfStars isFractional:(BOOL)isFract{
self = [super initWithFrame:frame];
if (self) {
+ isFractionalRatingEnabled = isFract;
numberOfStars = _numberOfStars;
+ if (isFractionalRatingEnabled)
+ numberOfStars *=kNumberOfFractions;
[self setupView];
}
return self;
@@ -170,16 +179,23 @@ - (void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
#pragma mark -
#pragma mark Rating Property
-- (void)setRating:(NSUInteger)_rating {
+- (void)setRating:(float)_rating {
+ if (isFractionalRatingEnabled) {
+ _rating *=kNumberOfFractions;
+ }
[self disableStarsDownTo:0];
currentIdx = _rating-1;
[self enableStarsUpTo:currentIdx];
}
-- (NSUInteger)rating {
+- (float)rating {
+ if (isFractionalRatingEnabled) {
+ return (float)(currentIdx+1)/kNumberOfFractions;
+ }
return (NSUInteger)currentIdx+1;
}
+
#pragma mark -
#pragma mark Memory Management
6 DLStarRating/DLStarView.h 100644 → 100755
View
@@ -15,11 +15,11 @@
#define kEdgeInsetBottom 20
@interface DLStarView : UIButton {
-
+
}
-- (id)initWithDefault:(UIImage*)star highlighted:(UIImage*)highlightedStar position:(int)index;
+- (id)initWithDefault:(UIImage*)star highlighted:(UIImage*)highlightedStar position:(int)index allowFractions:(BOOL)fractions;
- (void)centerIn:(CGRect)_frame with:(int)numberOfStars;
- (void)setStarImage:(UIImage*)starImage highlightedStarImage:(UIImage*)highlightedImage;
-
+- (UIImage *)croppedImage:(UIImage*)image;
@end
46 DLStarRating/DLStarView.m 100644 → 100755
View
@@ -1,28 +1,34 @@
/*
-
- DLStarRating
- Copyright (C) 2011 David Linsin <dlinsin@gmail.com>
-
- All rights reserved. This program and the accompanying materials
- are made available under the terms of the Eclipse Public License v1.0
- which accompanies this distribution, and is available at
- http://www.eclipse.org/legal/epl-v10.html
-
+
+ DLStarRating
+ Copyright (C) 2011 David Linsin <dlinsin@gmail.com>
+
+ All rights reserved. This program and the accompanying materials
+ are made available under the terms of the Eclipse Public License v1.0
+ which accompanies this distribution, and is available at
+ http://www.eclipse.org/legal/epl-v10.html
+
*/
#import "DLStarView.h"
-
+#import "DLStarRatingControl.h"
@implementation DLStarView
#pragma mark -
#pragma mark Initialization
-- (id)initWithDefault:(UIImage*)star highlighted:(UIImage*)highlightedStar position:(int)index {
- self = [super initWithFrame:CGRectMake((star.size.width*index), 0, star.size.width, star.size.height+kEdgeInsetBottom)];
+- (id)initWithDefault:(UIImage*)star highlighted:(UIImage*)highlightedStar position:(int)index allowFractions:(BOOL)fractions {
+ self = [super initWithFrame:CGRectZero];
+
if (self) {
+ [self setTag:index];
+ if (fractions) {
+ highlightedStar = [self croppedImage:highlightedStar];
+ star = [self croppedImage:star];
+ }
+ self.frame = CGRectMake((star.size.width*index), 0, star.size.width, star.size.height+kEdgeInsetBottom);
[self setStarImage:star highlightedStarImage:highlightedStar];
- [self setTag:index];
[self setImageEdgeInsets:UIEdgeInsetsMake(0, 0, kEdgeInsetBottom, 0)];
[self setBackgroundColor:[UIColor clearColor]];
if (index == 0) {
@@ -34,6 +40,20 @@ - (id)initWithDefault:(UIImage*)star highlighted:(UIImage*)highlightedStar posit
return self;
}
+
+- (UIImage *)croppedImage:(UIImage*)image {
+ float partWidth = image.size.width/kNumberOfFractions * image.scale;
+ int part = (self.tag+kNumberOfFractions)%kNumberOfFractions;
+ float xOffset = partWidth*part;
+ CGRect newFrame = CGRectMake(xOffset, 0, partWidth , image.size.height * image.scale);
+ CGImageRef resultImage = CGImageCreateWithImageInRect([image CGImage], newFrame);
+ UIImage *result = [UIImage imageWithCGImage:resultImage scale:image.scale orientation:image.imageOrientation];
+ CGImageRelease(resultImage);
+ return result;
+}
+
+
+
#pragma mark -
#pragma mark UIView methods
BIN  DLStarRating/images/star.png
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  DLStarRating/images/star@2x.png
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  DLStarRating/images/star_highlighted-darker.png
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  DLStarRating/images/star_highlighted-darker@2x.png
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  DLStarRating/images/star_highlighted.png
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  DLStarRating/images/star_highlighted@2x.png
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
0  DLStarRatingDemo/Classes/DLStarRatingDemoAppDelegate.h 100644 → 100755
View
File mode changed
0  DLStarRatingDemo/Classes/DLStarRatingDemoAppDelegate.m 100644 → 100755
View
File mode changed
0  DLStarRatingDemo/Classes/DLStarRatingDemoViewController.h 100644 → 100755
View
File mode changed
9 DLStarRatingDemo/Classes/DLStarRatingDemoViewController.m 100644 → 100755
View
@@ -23,10 +23,11 @@ - (void)viewDidLoad {
[super viewDidLoad];
// Custom Number of Stars
- DLStarRatingControl *customNumberOfStars = [[DLStarRatingControl alloc] initWithFrame:CGRectMake(0, 154, 320, 153) andStars:3];
+ DLStarRatingControl *customNumberOfStars = [[DLStarRatingControl alloc] initWithFrame:CGRectMake(0, 154, 320, 153) andStars:5 isFractional:YES];
+ customNumberOfStars.delegate = self;
customNumberOfStars.backgroundColor = [UIColor groupTableViewBackgroundColor];
customNumberOfStars.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
- customNumberOfStars.rating = 2;
+ customNumberOfStars.rating = 2.5;
[self.view addSubview:customNumberOfStars];
[customNumberOfStars release];
@@ -53,8 +54,8 @@ - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interface
#pragma mark -
#pragma mark Delegate implementation of NIB instatiated DLStarRatingControl
--(void)newRating:(DLStarRatingControl *)control :(NSUInteger)rating {
- self.stars.text = [NSString stringWithFormat:@"%d star rating",rating];
+-(void)newRating:(DLStarRatingControl *)control :(float)rating {
+ self.stars.text = [NSString stringWithFormat:@"%0.1f star rating",rating];
}
0  DLStarRatingDemo/DLStarRatingDemo-Info.plist 100644 → 100755
View
File mode changed
0  DLStarRatingDemo/DLStarRatingDemoViewController.xib 100644 → 100755
View
File mode changed
0  DLStarRatingDemo/DLStarRatingDemo_Prefix.pch 100644 → 100755
View
File mode changed
0  DLStarRatingDemo/MainWindow.xib 100644 → 100755
View
File mode changed
0  DLStarRatingDemo/main.m 100644 → 100755
View
File mode changed
Something went wrong with that request. Please try again.