//评价星星控件用法
StarView*startView = [[StarViewalloc]initWithFrame:CGRectMake(0,0,200,200)];
startView.startNum=4;
startView.defalutImage= [UIImageimageNamed:@""];
startView.selectedImage= [UIImageimageNamed:@""];
[self.viewaddSubview:startView];
星星控件:
#import "QLStarView.h"
//星星的大小
#define startBtnSize30.0
//星星的间隙
#define startInterval5.0
@interface QLStarView()
@property (nonatomic, strong) NSMutableArray *btnArr;
@end
@implementation QLStarView
- (instancetype)init {
self= [superinit];
if(self) {
[selfdefaultSet];
}
return self;
}
- (instancetype)initWithFrame:(CGRect)frame {
if(self== [superinitWithFrame:frame]) {
[selfdefaultSet];
}
return self;
}
- (void)defaultSet {
[self setBackgroundColor:[UIColor whiteColor]];
}
- (void)drawRect:(CGRect)rect {
self.tag=11;
CGFloatoriginX = (rect.size.width-_startNum*startBtnSize-(_startNum-1)*startInterval) *0.5;
CGFloatoriginY = (rect.size.height-startBtnSize)*0.5;
for(inti =0; i <_startNum; i++) {
QLBaseButton*startButton = [[QLBaseButtonalloc]initWithFrame:CGRectMake(originX + (startInterval+startBtnSize) * i, originY,startBtnSize,startBtnSize)];
startButton.light=NO;
startButton.tag= i ;
[startButtonsetImage:_defalutImage forState:UIControlStateNormal];
[startButtonsetImage:_selectedImage forState:UIControlStateSelected];
[startButtonaddTarget:self action:@selector(startBtnClick:) forControlEvents:UIControlEventTouchUpInside];
[selfaddSubview:startButton];
[self.btnArraddObject:startButton];
}
//点击手势
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
[self addGestureRecognizer:pan];
}
- (void)startBtnClick:(UIButton*)sender {
for(inti =0; i <_startNum; i++) {
if(i <= sender.tag) {
if(sender.tag==0) {
UIButton*button = [selfviewWithTag:i];
button.selected= !button.selected;
if(button.selected==YES) {
self.score=1;
}else{
self.score=0;
}
}else{
UIButton*button = [selfviewWithTag:i];
button.selected=YES;
self.score= sender.tag+1;
}
}else{
UIButton*button = [selfviewWithTag:i];
button.selected=NO;
}
}
}
- (void)pan:(UIPanGestureRecognizer *)gesture {
CGPointpoint = [gesturetranslationInView:self];
floatdistance = fabs(point.x);
NSInteger k = floor(distance/(startBtnSize+startInterval));
NSIntegerindex = -1;
if(point.x>=0) {
index = k;
}else{
index =self.score- k;
if(index <0) {
index =0;
}
}
if(index >=0&&index
UIButton*button = [selfviewWithTag:index];
[selfstartBtnClick:button];
}
}
#pragma mark- lazyLoading
- (NSMutableArray*)btnArr {
if(!_btnArr) {
_btnArr= [NSMutableArrayarray];
}
return _btnArr;
}
@end