Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update hilight image when tocuh begin #17

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
58 changes: 56 additions & 2 deletions EDStarRating/EDStarRating.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,60 @@
#endif


//
// ARC Helper
//
// Version 1.2.1
//
// Created by Nick Lockwood on 05/01/2012.
// Copyright 2012 Charcoal Design
//
// Distributed under the permissive zlib license
// Get the latest version from here:
//
// https://gist.github.com/1563325
//

#ifndef AH_RETAIN
#if __has_feature(objc_arc)
#define AH_RETAIN(x) (x)
#define AH_RELEASE(x)
#define AH_AUTORELEASE(x) (x)
#define AH_SUPER_DEALLOC
#else
#define __AH_WEAK
#define AH_WEAK assign
#define AH_RETAIN(x) [(x) retain]
#define AH_RELEASE(x) [(x) release]
#define AH_AUTORELEASE(x) [(x) autorelease]
#define AH_SUPER_DEALLOC [super dealloc]
#endif
#endif

// Weak reference support

#ifndef AH_WEAK
#if defined __IPHONE_OS_VERSION_MIN_REQUIRED
#if __IPHONE_OS_VERSION_MIN_REQUIRED > __IPHONE_4_3
#define __AH_WEAK __weak
#define AH_WEAK weak
#else
#define __AH_WEAK __unsafe_unretained
#define AH_WEAK unsafe_unretained
#endif
#elif defined __MAC_OS_X_VERSION_MIN_REQUIRED
#if __MAC_OS_X_VERSION_MIN_REQUIRED > __MAC_10_7
#define __AH_WEAK __weak
#define AH_WEAK weak
#else
#define __AH_WEAK __unsafe_unretained
#define AH_WEAK unsafe_unretained
#endif
#endif
#endif

// ARC Helper ends


#if EDSTAR_MACOSX
#import <Cocoa/Cocoa.h>
Expand Down Expand Up @@ -61,15 +115,15 @@ typedef UIImage EDImage;
@property (nonatomic) EDStarRatingDisplayMode displayMode;
@property (nonatomic) float halfStarThreshold;

@property (nonatomic,weak) id<EDStarRatingProtocol> delegate;
@property (nonatomic,AH_WEAK) id<EDStarRatingProtocol> delegate;
@property (nonatomic,copy) EDStarRatingReturnBlock returnBlock;
@end


@protocol EDStarRatingProtocol <NSObject>

@optional
-(void)starsSelectionChanged:(EDStarRating*)control rating:(float)rating;
-(UIImage *)starsSelectionChanged:(EDStarRating*)control rating:(float)rating;

@end

128 changes: 37 additions & 91 deletions EDStarRating/EDStarRating.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,6 @@

#define ED_DEFAULT_HALFSTAR_THRESHOLD 0.6


@interface EDStarRating()
@property (nonatomic,strong ) EDImage *tintedStarImage;
@property (nonatomic,strong ) EDImage *tintedStarHighlightedImage;
@property (nonatomic) CGColorRef backCGColor;
@end


@implementation EDStarRating
@synthesize starImage;
@synthesize starHighlightedImage;
Expand All @@ -29,7 +21,7 @@ @implementation EDStarRating
@synthesize halfStarThreshold;
@synthesize displayMode;
#if EDSTAR_MACOSX
@synthesize backgroundColor=_backgroundColor;
@synthesize backgroundColor;
#endif
@synthesize returnBlock=_returnBlock;

Expand All @@ -44,7 +36,6 @@ -(void)setDefaultProperties
horizontalMargin=10.0;
displayMode = EDStarRatingDisplayFull;
halfStarThreshold=ED_DEFAULT_HALFSTAR_THRESHOLD;
[self setBackgroundColor:[EDColor clearColor]];

}
#if EDSTAR_MACOSX
Expand Down Expand Up @@ -83,7 +74,19 @@ -(id)initWithFrame:(CGRect)frame



-(void)dealloc
{
AH_RELEASE(starImage);
AH_RELEASE(starHighlightedImage);
AH_RELEASE(backgroundImage);
#if EDSTAR_MACOSX
AH_RELEASE(backgroundColor);
#endif

#if ! __has_feature(objc_arc)
[super dealloc];
#endif
}
#pragma mark -
#pragma mark Setters
-(void)setReturnBlock:(EDStarRatingReturnBlock)retBlock
Expand Down Expand Up @@ -156,21 +159,6 @@ -(void)drawImage:(EDImage*)image atPosition:(NSInteger)position

}


-(void)setBackgroundColor:(EDColor *)color
{
#if EDSTAR_IOS
[super setBackgroundColor:color];
#else
_backgroundColor = color;
#endif
if( color )
{
self.backCGColor = [self cgColor:color];
}
}


-(CGColorRef)cgColor:(EDColor*)color
{
CGColorRef cgColor = nil;
Expand All @@ -182,8 +170,11 @@ -(CGColorRef)cgColor:(EDColor*)color
CGColorSpaceRef colorSpace = [[color colorSpace] CGColorSpace];

[color getComponents:(CGFloat *)&components];
cgColor = CGColorCreate(colorSpace, components);
CGColorSpaceRelease(colorSpace);
#if __has_feature(objc_arc)
cgColor = (__bridge CGColorRef)AH_AUTORELEASE((__bridge id)CGColorCreate(colorSpace, components));
#else
cgColor = ( CGColorRef)AH_AUTORELEASE(( id)CGColorCreate(colorSpace, components));
#endif

#else
cgColor = color.CGColor;
Expand All @@ -210,7 +201,8 @@ -(void)drawRect:(CGRect)rect
CGContextRef ctx = [self currentContext];

// Fill background color
CGContextSetFillColorWithColor(ctx, self.backCGColor);
EDColor *colorToDraw = self.backgroundColor==nil?[EDColor clearColor]:self.backgroundColor;
CGContextSetFillColorWithColor(ctx, [self cgColor:colorToDraw]);
CGContextFillRect(ctx, bounds);

// Draw background Image
Expand All @@ -223,7 +215,7 @@ -(void)drawRect:(CGRect)rect
CGSize starSize = starHighlightedImage.size;
for( NSInteger i=0 ; i<maxRating; i++ )
{
[self drawImage:self.tintedStarImage atPosition:i];
[self drawImage:self.starImage atPosition:i];
if( i < _rating ) // Highlight
{
CGContextSaveGState(ctx);
Expand Down Expand Up @@ -252,12 +244,11 @@ -(void)drawRect:(CGRect)rect

}

[self drawImage:self.tintedStarHighlightedImage atPosition:i];
[self drawImage:starHighlightedImage atPosition:i];
}
CGContextRestoreGState(ctx);
}
}

}


Expand Down Expand Up @@ -333,7 +324,15 @@ -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView:self];
self.rating =[self starsForPoint:touchLocation];
self.rating =[self starsForPoint:touchLocation];

if( self.delegate && [self.delegate respondsToSelector:@selector(starsSelectionChanged:rating:)] ){
UIImage * img = [self.delegate starsSelectionChanged:self rating:self.rating];
if (img != nil) {
starHighlightedImage = img;
}
}

[self setNeedsDisplay];
}

Expand All @@ -348,68 +347,15 @@ - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
if( !editable )
return;

if( self.delegate && [self.delegate respondsToSelector:@selector(starsSelectionChanged:rating:)] )
[self.delegate starsSelectionChanged:self rating:self.rating];
if( self.delegate && [self.delegate respondsToSelector:@selector(starsSelectionChanged:rating:)] ){
UIImage * img = [self.delegate starsSelectionChanged:self rating:self.rating];
if (img != nil) {
starHighlightedImage = img;
}
}

if( self.returnBlock)
self.returnBlock(self.rating);

}


#pragma mark - Tint color Support
-(void)setStarImage:(EDImage *)image
{
if( starImage == image)
return;

starImage = image;
self.tintedStarImage = [self tintedImage:image];
}

-(void)setStarHighlightedImage:(EDImage *)image
{
if( starHighlightedImage == image )
return;

starHighlightedImage = image;
self.tintedStarHighlightedImage = [self tintedImage:image];

}
-(EDImage*)tintedImage:(EDImage*)img
{
EDImage *tintedImage = img;
#if EDSTAR_IOS
// Make sure tintColor is available (>= iOS 7.0 runtime)
if( [self respondsToSelector:@selector(tintColor)] && img.renderingMode == UIImageRenderingModeAlwaysTemplate )
{

UIGraphicsBeginImageContextWithOptions(img.size, NO, [UIScreen mainScreen].scale );
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 0, img.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGRect rect = CGRectMake(0, 0, img.size.width, img.size.height);
// draw alpha-mask
CGContextSetBlendMode(context, kCGBlendModeNormal);
CGContextDrawImage(context, rect, img.CGImage);
// draw tint color, preserving alpha values of original image
CGContextSetBlendMode(context, kCGBlendModeSourceIn);
[self.tintColor setFill];
CGContextFillRect(context, rect);

tintedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
#endif
return tintedImage;
}
#if EDSTAR_IOS
-(void)tintColorDidChange
{
self.tintedStarImage = [self tintedImage:self.starImage];
self.tintedStarHighlightedImage = [self tintedImage:self.starHighlightedImage];
[self setNeedsDisplay];
}
#endif

@end
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ But it's only tested it with ARC enabled in 10.8+ and iOS 6.0+. Let me now if it

![EDStarRating](https://github.com/erndev/EDStarRating/raw/master/edstarrating.png)
![EDStarRating](https://github.com/erndev/EDStarRating/raw/master/edstarrating-ios.png)
![EDStarRating](https://github.com/beiliubei/EDStarRating/raw/master/edstarrating.gif)

### License
BSD License.
Expand Down
Binary file added edstarrating.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.