diff --git a/EDStarRating/EDStarRating.h b/EDStarRating/EDStarRating.h index d23151c..dfee110 100755 --- a/EDStarRating/EDStarRating.h +++ b/EDStarRating/EDStarRating.h @@ -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 @@ -61,7 +115,7 @@ typedef UIImage EDImage; @property (nonatomic) EDStarRatingDisplayMode displayMode; @property (nonatomic) float halfStarThreshold; -@property (nonatomic,weak) id delegate; +@property (nonatomic,AH_WEAK) id delegate; @property (nonatomic,copy) EDStarRatingReturnBlock returnBlock; @end @@ -69,7 +123,7 @@ typedef UIImage EDImage; @protocol EDStarRatingProtocol @optional --(void)starsSelectionChanged:(EDStarRating*)control rating:(float)rating; +-(UIImage *)starsSelectionChanged:(EDStarRating*)control rating:(float)rating; @end diff --git a/EDStarRating/EDStarRating.m b/EDStarRating/EDStarRating.m index 7b5ad0e..c3e3b71 100755 --- a/EDStarRating/EDStarRating.m +++ b/EDStarRating/EDStarRating.m @@ -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; @@ -29,7 +21,7 @@ @implementation EDStarRating @synthesize halfStarThreshold; @synthesize displayMode; #if EDSTAR_MACOSX -@synthesize backgroundColor=_backgroundColor; +@synthesize backgroundColor; #endif @synthesize returnBlock=_returnBlock; @@ -44,7 +36,6 @@ -(void)setDefaultProperties horizontalMargin=10.0; displayMode = EDStarRatingDisplayFull; halfStarThreshold=ED_DEFAULT_HALFSTAR_THRESHOLD; - [self setBackgroundColor:[EDColor clearColor]]; } #if EDSTAR_MACOSX @@ -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 @@ -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; @@ -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; @@ -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 @@ -223,7 +215,7 @@ -(void)drawRect:(CGRect)rect CGSize starSize = starHighlightedImage.size; for( NSInteger i=0 ; i= 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 diff --git a/README.md b/README.md index 3014370..b67d7f6 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/edstarrating.gif b/edstarrating.gif new file mode 100644 index 0000000..f4c989c Binary files /dev/null and b/edstarrating.gif differ