在iOS里实现高机能的评分控件-
前言
做为老司机的你们有没有碰到过这样的需求?每个商品或者商家的item都有个星级或者其他评分,大约像下列的结果图
实现方案:
大神本人写个通用空间(在工夫充沛的状况下)
网上寻个比拼好的第三方 (工夫比拼紧凑的状况下)
更直接的,本人直接放几个ImageView或者Layer
思索:功能是实现了,但是机能宛如有点挨影响。概括缘由要看第三方框架的实现道理,固然了也有做的非常不错的。我是个机能控,当我拿到这个需求的时候,也尝试用一些第三方,但效果不尽人意。最后XWStarView就此发生了。
XWStarView(高机能星星控件)
举荐理由:
简略易用
高机能,采纳yyLabel异步绘制
支撑自定义星星样式,间距
局限性:
当前只支撑半星,一星评分
当前只支撑图片
依赖YYLabel
XWStarMaker(外不雅配置)
开发者可以配置间距,最大值,默许图片,选择图片
@interface XWStarMaker : NSObject @property (nonatomic, assign) CGFloat space; @property (nonatomic, strong) NSString *defaultImage; @property (nonatomic, strong) NSString *selectImage; @property (nonatomic,assign) NSInteger maxValue; @end
XWStarView.m(中心代码)
眼尖的同窗已经看到了,XWStarView直接继承了YYLabel,熟知YYLaebl的开发者可能晓得我要干嘛了。
#import "YYLabel.h" #import "XWStarMaker.h" @class XWStarView; @protocol XWStarViewDelegate@optional -(void)xw_starView:(XWStarView*)tagView star:(NSInteger)star; @end @interface XWStarView : YYLabel @property (nonatomic, assign) NSInteger score; @property (nonatomic,weak) id delegate; -(instancetype)initWithFrame:(CGRect)frame maker:(void (^)(XWStarMaker *))makeBlock; @end
概括的实现细节看.m文件
@interface XWStarView () @property (nonatomic,strong) XWStarMaker *maker; @end @implementation XWStarView -(instancetype)initWithFrame:(CGRect)frame maker:(void (^)(XWStarMaker *))makeBlock{ if (self = [super initWithFrame:frame]) { self.maker = [[XWStarMaker alloc] init]; if (makeBlock) { makeBlock(self.maker); } self.displaysAsynchronously = YES; self.fadeOnAsynchronouslyDisplay = NO; [self creatScoreAttr]; } return self; } #pragma mark - private -(void)creatScoreAttr{ NSMutableAttributedString *text = [NSMutableAttributedString new]; UIFont *font = [UIFont systemFontOfSize:0]; for (int i = 0; i < self.maker.maxValue; i++) { UIImage *image = [UIImage imageNamed:self.maker.defaultImage]; NSMutableAttributedString *attachText = [NSMutableAttributedString yy_attachmentStringWithContent:image contentMode:UIViewContentModeLeft attachmentSize:CGSizeMake(image.size.width + self.maker.space, image.size.height) alignToFont:font alignment:YYTextVerticalAlignmentCenter]; //添加点击事件 weak typeof(&*self) weakSelf = self; [attachText yy_setTextHighlightRange:NSMakeRange(0, 1) color:nil backgroundColor:nil tapAction:^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect){ if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(xw_starView:star:)]) { [weakSelf.delegate xw_starView:weakSelf star:i]; } }]; [text appendAttributedString:attachText]; } self.attributedText = text; } -(void)setScore:(NSInteger)score{ if (_score == score) { return; } _score = score; //获取图片资源 NSArray *attachments = self.textLayout.attachments; for (int i = 0; i < attachments.count; i++) { YYTextAttachment *attachment = attachments[i]; attachment.content = [UIImage imageNamed:i <= _score ? self.maker.selectImage : self.maker.defaultImage]; } } @end
只有你是个iOS程序员大约都看得懂代码吧。实现很简略,但是结果却纷歧般,特殊在复杂列表运用的时候很显明。
XWStarView运用
_scoreView = [[XWStarView alloc] initWithFrame:CGRectMake(0, self.frame.size.height - 40, self.frame.size.width, 40) maker:^(XWStarMaker *maker){ maker.defaultImage = @"goods_score_empt.png"; maker.selectImage = @"goods_score_full.png"; maker.space = 10; }]; _scoreView.delegate = self;
XWStarView是YYLabel的喜爱者不错的选中哦,要是知足你的业务需求,机能方面会让你很中意的,不信你就试试(哈哈,俏皮了)。固然了萝卜青菜各有所爱,不喜勿喷。
信赖看了本案牍例你已经把握了办法,更多出色请关注 百分百源码网 其它相干文章!
举荐浏览:
H5的存储方式详解
H5怎样运用束缚验证API
以上就是在iOS里实现高机能的评分控件的细致内容,更多请关注 百分百源码网 其它相干文章!