![](https://img.haomeiwen.com/i6065477/f739c86f4e2c51e4.png)
//评价星星控件用法
StarView*startView = [[StarViewalloc]initWithFrame:CGRectMake(0,0,200,200)];
startView.startNum=4;
startView.defalutImage= [UIImageimageNamed:@""];
startView.selectedImage= [UIImageimageNamed:@""];
[self.viewaddSubview:startView];
星星控件:
#import"StarView.h"
//星星的大小
#define startBtnSize 30
//星星的间隙
#define startInterval 5
@implementationStarView
- (instancetype)initWithFrame:(CGRect)frame {
if(self== [superinitWithFrame:frame]) {
[selfsetBackgroundColor:[UIColorwhiteColor]];
}
returnself;
}
- (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++) {
UIButton*startButton = [[UIButtonalloc]initWithFrame:CGRectMake(originX + (startInterval+startBtnSize) * i, originY,startBtnSize,startBtnSize)];
startButton.tag= i ;
[startButtonsetImage:_defalutImageforState:UIControlStateNormal];
[startButtonsetImage:_selectedImageforState:UIControlStateSelected];
[startButtonaddTarget:selfaction:@selector(startBtnClick:)forControlEvents:UIControlEventTouchUpInside];
[selfaddSubview:startButton];
}
//点击手势
UIPanGestureRecognizer*pan = [[UIPanGestureRecognizeralloc]initWithTarget:selfaction:@selector(pan:)];
[selfaddGestureRecognizer: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;
}
}else{
UIButton*button = [selfviewWithTag:i];
button.selected=NO;
}
}
}
- (void)pan:(UIPanGestureRecognizer*)gesture {
CGPointpoint = [gesture locationInView:self];
if(point.x>0&& point.x<_startNum*startBtnSize+(_startNum-1)*startInterval) {
CGFloatk = point.x/(startBtnSize+startInterval);
for(inti =0; i <_startNum; i++) {
if(k >= i && k < i+1) {
k = i;
}
}
UIButton*button = [selfviewWithTag:k];
[selfstartBtnClick:button];
}
}
@end
评价星星控件下载地址:https://pan.baidu.com/s/1jIJsnDc(不好的地方,勿喷)
网友评论