Created
May 14, 2012 21:05
-
-
Save georgejecook/2697011 to your computer and use it in GitHub Desktop.
starview is coming out black when scale is smaller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Created by georgecook on 13/05/2012. | |
// | |
// To change the template use AppCode | Preferences | File Templates. | |
// | |
#import "TTStarView.h" | |
#import "UIView+FrameAdditions.h" | |
#define kMaxStars 5 | |
#define kStarWidth 47 | |
#define kStarHeight 45 | |
#define kGap 5 | |
static UIImage *_starHighlightedImage; | |
static UIImage *_starImage; | |
@interface TTStarView () | |
@end | |
@implementation TTStarView { | |
} | |
@synthesize rating = _rating; | |
@synthesize starViewSize = _starViewSize; | |
+ (void)initialize { | |
_starImage = [UIImage imageNamed:@"star.png"]; | |
_starHighlightedImage = [UIImage imageNamed:@"star_highlighted.png"]; | |
} | |
-(id)initWithOrigin:(CGPoint)origin withSize:(TTStarViewSize)starViewSize{ | |
self = [super initWithFrame:CGRectMake(origin.x, origin.y, 0, 0)]; | |
if (self) { | |
self.starViewSize = starViewSize; | |
} | |
return self; | |
} | |
#pragma mark - shared init | |
- (void)sharedInit { | |
self.backgroundColor = [UIColor clearColor]; | |
_starViewSize = TTStarViewSizeTiny; | |
} | |
- (id)initWithFrame:(CGRect)frame { | |
self = [super initWithFrame:frame]; | |
if (self) { | |
[self sharedInit]; | |
} | |
return self; | |
} | |
- (void)awakeFromNib { | |
[super awakeFromNib]; | |
[self sharedInit]; | |
} | |
#pragma mark licecycle | |
- (void)dealloc { | |
} | |
- (void)drawRect:(CGRect)rect { | |
[super drawRect:rect]; | |
[self setBackgroundColor:[UIColor clearColor]]; | |
//draw the normal stars.. | |
int i; | |
float x = rect.origin.x; | |
float y = rect.origin.y; | |
float starScale = _starViewSize / 100.0f; | |
float starWidth = kStarWidth * starScale; | |
float starHeight = kStarHeight * starScale; | |
float gap = kGap * starScale; | |
for (i =0; i < _rating; i++){ | |
[_starHighlightedImage drawInRect:CGRectMake(x, y, starWidth, starHeight)]; | |
x+= gap + starWidth; | |
} | |
x = rect.origin.x; | |
//draw selected stars | |
for (i = (int) _rating; i < kMaxStars; i++){ | |
[_starImage drawInRect:CGRectMake(x, y, starWidth, starHeight)]; | |
x+= gap + starWidth; | |
} | |
} | |
#pragma mark public api | |
- (void)setRating:(float)aRating { | |
_rating = aRating; | |
[self setNeedsDisplay]; | |
} | |
- (void)setStarViewSize:(TTStarViewSize)aStarViewSize { | |
_starViewSize = aStarViewSize; | |
float starScale = aStarViewSize / 100.0f; | |
self.frame = CGRectMake(self.x, self.y, ((kStarWidth + kGap) * kMaxStars) *starScale, (kStarHeight) * starScale); | |
[self setNeedsLayout]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment