星级评论控件
.m文件
#import "Start.h"
@implementation Start
-(void)setPercent:(CGFloat)percent
{
_percent = percent;
[self setNeedsDisplay];
}
-(void)drawRect:(CGRect)rect
{
[super drawRect:rect];
UIImage *image = [UIImage imageNamed:@"start"];
for (int i = 0; i < 5; i++) {
CGRect newRect = CGRectMake(i*0.2*rect.size.width, 0, 0.2*rect.size.width, rect.size.height );
[image drawInRect:newRect];
}
[[UIColor whiteColor] set];
UIRectFillUsingBlendMode(rect, kCGBlendModeSourceIn);
CGRect newRect= CGRectMake(0, 0, self.percent*rect.size.width, rect.size.height);
[[UIColor orangeColor] set];
UIRectFillUsingBlendMode(newRect, kCGBlendModeSourceIn);
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
CGFloat xPoint = [[touches anyObject]locationInView:self].x;
[self setPercent:xPoint/self.bounds.size.width];
}
@end
.h文件
#import
@interface Start : UIView
@property(nonatomic,assign)CGFloat percent;
@end
demo代码地址:https://yunpan.cn/cReHb4CSMdtdj (提取码:5cd5)