Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
cf2813d
modified star control to display partial ratings
xeniah Mar 24, 2014
a313459
started to edit README. Deleted files
xeniah Mar 24, 2014
2c9a9e3
Delete AMRatingControl-Info.plist
xeniah Mar 24, 2014
0de77ae
Delete AMRatingControl.podspec
xeniah Mar 24, 2014
4860cfa
Delete AMRatingControl_Prefix.pch
xeniah Mar 24, 2014
4a864ae
Delete contents.xcworkspacedata
xeniah Mar 24, 2014
72d43ca
Delete project.pbxproj
xeniah Mar 24, 2014
bb46eaa
Delete AMRatingControlAppDelegate.h
xeniah Mar 24, 2014
b037593
Delete AMRatingControlAppDelegate.m
xeniah Mar 24, 2014
cd268d5
Delete AMRatingControlViewController.h
xeniah Mar 24, 2014
70bcccc
Delete AMRatingControlViewController.m
xeniah Mar 24, 2014
d67d33f
Delete AMRatingControlViewController.xib
xeniah Mar 24, 2014
b4c8ab0
Delete AMRatingControl.h
xeniah Mar 24, 2014
bb86309
Delete AMRatingControl.m
xeniah Mar 24, 2014
591f6b5
Update README.mdown
xeniah Mar 24, 2014
c08bf2e
for half ratings, have a option for 'half filled star' image
xeniah Mar 25, 2014
9a78da5
Merge branch 'master' of github.com:xeniah/StarRatingControl
xeniah Mar 25, 2014
b5a2f43
added different default star images. Simplified sigs of the piublic i…
xeniah Mar 26, 2014
6d9cd32
Delete dot.png
xeniah Mar 26, 2014
b89d158
Delete empty_star.png
xeniah Mar 26, 2014
8484660
Delete empty_star@2x.png
xeniah Mar 26, 2014
279de1c
Delete full_star.png
xeniah Mar 26, 2014
1cc57a9
Delete full_star@2x.png
xeniah Mar 26, 2014
3bb92db
Delete half_star.png
xeniah Mar 26, 2014
8b93f1b
Delete half_star@2x.png
xeniah Mar 26, 2014
94668d3
Delete star.png
xeniah Mar 26, 2014
3a13f41
Update README.mdown
xeniah Mar 26, 2014
d120a76
updated license
xeniah Mar 26, 2014
de7eac0
Merge branch 'master' of github.com:xeniah/StarRatingControl
xeniah Mar 26, 2014
1a00ec2
slight edits of the sample app. Add screenshot
xeniah Mar 27, 2014
5ce680a
Update README.mdown
xeniah Mar 27, 2014
2081360
Moved images to Resources subdir
xeniah Mar 27, 2014
478ef7b
Merge branch 'master' of github.com:xeniah/StarRatingControl
xeniah Mar 27, 2014
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
24 changes: 0 additions & 24 deletions AMRatingControl.podspec

This file was deleted.

21 changes: 18 additions & 3 deletions Classes/AMRatingControl.h → Classes/StarRatingControl.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// AMRatingControl.h
// StarRatingControl.h
// RatingControl
//

Expand All @@ -10,14 +10,14 @@ typedef void (^EditingChangedBlock)(NSUInteger rating);
typedef void (^EditingDidEndBlock)(NSUInteger rating);


@interface AMRatingControl : UIControl
@interface StarRatingControl : UIControl


/**************************************************************************************************/
#pragma mark - Getters and Setters

@property (nonatomic, assign) NSInteger maxRating;
@property (nonatomic, assign) NSInteger rating;
@property (nonatomic, assign) float rating;
@property (nonatomic, readwrite) NSUInteger starFontSize;
@property (nonatomic, readwrite) NSUInteger starWidthAndHeight;
@property (nonatomic, readwrite) NSUInteger starSpacing;
Expand Down Expand Up @@ -57,6 +57,21 @@ typedef void (^EditingDidEndBlock)(NSUInteger rating);
solidImage:(UIImage *)solidImageOrNil
andMaxRating:(NSInteger)maxRating;

/**
* @param location : position of the rating control in your view
* The control will manage its own width/height (kind of like UIActivityIndicator)
* @param emptyImage & solidImage can both be nil, or not even a dot or a star (a any images you want!)
* If either of these parameters are nil, the class will draw its own stars
* @param userInteractionEnabled - self explanatory
* @param initialRating will initialize the number of stars and partial stars to show with the control at startup
* @param maxRating
*
*/
- (id)initWithLocation:(CGPoint)location
emptyImage:(UIImage *)emptyImageOrNil
solidImage:(UIImage *)solidImageOrNil
initialRating:(float)initialRating
andMaxRating:(NSInteger)maxRating;


@end
160 changes: 139 additions & 21 deletions Classes/AMRatingControl.m → Classes/StarRatingControl.m
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
//
// AMRatingControl.m
// StarRatingControl.m
// RatingControl
//


#import "AMRatingControl.h"
#import "StarRatingControl.h"


// Constants :
static const CGFloat kFontSize = 20;
static const NSInteger kStarWidthAndHeight = 20;
static const NSInteger kStarWidthAndHeight = 27;
static const NSInteger kStarSpacing = 0;

static const NSString *kDefaultEmptyChar = @"☆";
static const NSString *kDefaultSolidChar = @"★";


@interface AMRatingControl (Private)
@interface StarRatingControl (Private)

- (id)initWithLocation:(CGPoint)location
emptyImage:(UIImage *)emptyImageOrNil
Expand All @@ -31,12 +30,13 @@ - (void)handleTouch:(UITouch *)touch;
@end


@implementation AMRatingControl
@implementation StarRatingControl
{
BOOL _respondsToTranslatesAutoresizingMaskIntoConstraints;
UIImage *_emptyImage, *_solidImage;
UIColor *_emptyColor, *_solidColor;
NSInteger _maxRating;
BOOL _partialStarsAllowed;
}

/**************************************************************************************************/
Expand All @@ -52,7 +52,7 @@ - (void)setMaxRating:(NSInteger)maxRating
[self setNeedsDisplay];
}

- (void)setRating:(NSInteger)rating
- (void)setRating:(float)rating
{
_rating = (rating < 0) ? 0 : rating;
_rating = (rating > _maxRating) ? _maxRating : rating;
Expand Down Expand Up @@ -99,6 +99,23 @@ - (id)initWithLocation:(CGPoint)location
andMaxRating:maxRating];
}


- (id)initWithLocation:(CGPoint)location
emptyImage:(UIImage *)emptyImageOrNil
solidImage:(UIImage *)solidImageOrNil
initialRating:(float)initialRating
andMaxRating:(NSInteger)maxRating
{
return [self initWithLocation:location
emptyImage:emptyImageOrNil
solidImage:solidImageOrNil
emptyColor:nil
solidColor:nil
initialRating:initialRating
andMaxRating:maxRating];
}


- (id)initWithLocation:(CGPoint)location
emptyColor:(UIColor *)emptyColor
solidColor:(UIColor *)solidColor
Expand Down Expand Up @@ -136,9 +153,12 @@ - (CGSize)intrinsicContentSize
- (void)drawRect:(CGRect)rect
{
CGPoint currPoint = CGPointZero;
int wholeStars = (int)floor(_rating);
float partialStars = _rating - (float)wholeStars;

for (int i = 0; i < _rating; i++)
for (int i = 0; i < wholeStars; i++)
{

if (_solidImage)
{
[_solidImage drawAtPoint:currPoint];
Expand All @@ -151,8 +171,15 @@ - (void)drawRect:(CGRect)rect

currPoint.x += (_starWidthAndHeight + _starSpacing);
}

NSInteger remaining = _maxRating - _rating;

if (partialStars > 0) {
UIImage *partialStar = [self partialImage:_solidImage fraction:partialStars];
[_emptyImage drawAtPoint:currPoint];
[partialStar drawAtPoint:currPoint];
currPoint.x += (_starWidthAndHeight + _starSpacing);
}

NSInteger remaining = (floor)(_maxRating - _rating) ;

for (int i = 0; i < remaining; i++)
{
Expand Down Expand Up @@ -197,15 +224,30 @@ - (void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
/**************************************************************************************************/
#pragma mark - Private Methods


- (void)initializeWithEmptyImage:(UIImage *)emptyImageOrNil
solidImage:(UIImage *)solidImageOrNil
emptyColor:(UIColor *)emptyColor
solidColor:(UIColor *)solidColor
andMaxRating:(NSInteger)maxRating
{
[self initializeWithEmptyImage:emptyImageOrNil
solidImage:solidImageOrNil
emptyColor:emptyColor
solidColor:solidColor
initialRating:0.0
andMaxRating:maxRating];
}

- (void)initializeWithEmptyImage:(UIImage *)emptyImageOrNil
solidImage:(UIImage *)solidImageOrNil
emptyColor:(UIColor *)emptyColor
solidColor:(UIColor *)solidColor
initialRating:(float)initialRating
andMaxRating:(NSInteger)maxRating
{
_respondsToTranslatesAutoresizingMaskIntoConstraints = [self respondsToSelector:@selector(translatesAutoresizingMaskIntoConstraints)];

_rating = 0;
self.backgroundColor = [UIColor clearColor];
self.opaque = NO;

Expand All @@ -214,9 +256,15 @@ - (void)initializeWithEmptyImage:(UIImage *)emptyImageOrNil
_emptyColor = emptyColor;
_solidColor = solidColor;
_maxRating = maxRating;
_rating = initialRating;
_starFontSize = kFontSize;
_starWidthAndHeight = kStarWidthAndHeight;
_starSpacing = kStarSpacing;

if(!_emptyColor && !_solidColor && _solidImage)
{
_partialStarsAllowed = YES;
}
}

- (id)initWithCoder:(NSCoder *)decoder
Expand All @@ -227,12 +275,37 @@ - (id)initWithCoder:(NSCoder *)decoder
solidImage:nil
emptyColor:nil
solidColor:nil
initialRating:0.0
andMaxRating:0];
}
return self;
}


- (id)initWithLocation:(CGPoint)location
emptyImage:(UIImage *)emptyImageOrNil
solidImage:(UIImage *)solidImageOrNil
emptyColor:(UIColor *)emptyColor
solidColor:(UIColor *)solidColor
initialRating:(float)initialRating
andMaxRating:(NSInteger)maxRating
{
if (self = [self initWithFrame:CGRectMake(location.x,
location.y,
(maxRating * kStarWidthAndHeight),
kStarWidthAndHeight)])
{
[self initializeWithEmptyImage:emptyImageOrNil
solidImage:solidImageOrNil
emptyColor:emptyColor
solidColor:solidColor
initialRating:(float)initialRating
andMaxRating:maxRating];
}

return self;
}

- (id)initWithLocation:(CGPoint)location
emptyImage:(UIImage *)emptyImageOrNil
solidImage:(UIImage *)solidImageOrNil
Expand All @@ -249,12 +322,14 @@ - (id)initWithLocation:(CGPoint)location
solidImage:solidImageOrNil
emptyColor:emptyColor
solidColor:solidColor
initialRating:0.0f
andMaxRating:maxRating];
}

return self;
}


- (void)adjustFrame
{
if (_respondsToTranslatesAutoresizingMaskIntoConstraints && !self.translatesAutoresizingMaskIntoConstraints)
Expand All @@ -278,7 +353,7 @@ - (void)handleTouch:(UITouch *)touch

CGPoint touchLocation = [touch locationInView:self];

if (touchLocation.x < 0)
if (touchLocation.x < 0 || touchLocation.x < ((float)kStarWidthAndHeight)/3.0)
{
if (_rating != 0)
{
Expand All @@ -302,24 +377,67 @@ - (void)handleTouch:(UITouch *)touch
}
else
{
float halfWidth = (float)_starWidthAndHeight/2.0;

for (int i = 0 ; i < _maxRating ; i++)
{
if ((touchLocation.x > section.origin.x) && (touchLocation.x < (section.origin.x + _starWidthAndHeight)))
if (touchLocation.x > section.origin.x)
{
if (_rating != (i + 1))
{
_rating = i + 1;
if (self.editingChangedBlock)
{
self.editingChangedBlock(_rating);
if (_partialStarsAllowed) {
// first half of the star
if (touchLocation.x < (section.origin.x + halfWidth)) {
if (_rating != (i + 0.5))
{
_rating = i + 0.5;
if (self.editingChangedBlock)
{
self.editingChangedBlock(_rating);
}
}
break;
}

// second half of the star
if (touchLocation.x > (section.origin.x + halfWidth) &&
touchLocation.x < (section.origin.x + _starWidthAndHeight)) {
if (_rating != (i + 1))
{
_rating = i + 1;
if (self.editingChangedBlock)
{
self.editingChangedBlock(_rating);
}
}
break;
}
}else{ // only wholestars
if (touchLocation.x < (section.origin.x + _starWidthAndHeight)) {
if (_rating != (i + 1))
{
_rating = i + 1;
if (self.editingChangedBlock)
{
self.editingChangedBlock(_rating);
}
}
break;
}
}
break;
}
}
section.origin.x += (_starWidthAndHeight + _starSpacing);
}
}
[self setNeedsDisplay];
}

- (UIImage *) partialImage:(UIImage *)image fraction:(float)fraction
{
CGImageRef imgRef = image.CGImage;
CGImageRef fractionalImgRef = CGImageCreateWithImageInRect(imgRef, CGRectMake(0, 0, image.size.width * fraction, image.size.height));
UIImage *fractionalImg = [UIImage imageWithCGImage:fractionalImgRef];
CGImageRelease(fractionalImgRef);
return fractionalImg;
}

@end
21 changes: 0 additions & 21 deletions Demo/App/AMRatingControlAppDelegate.h

This file was deleted.

Loading