iOS實現(xiàn)高性能簡單易用的星星評分控件
前言
做為老司機(jī)的你們有沒有遇到過這樣的需求?每個商品或者商家的item都有個星級或者其他評分,大概像以下的效果圖

實現(xiàn)方案:
- 大神自己寫個通用空間(在時間充足的情況下)
- 網(wǎng)上找個比較好的第三方 (時間比較緊湊的情況下)
- 更直接的,自己直接放幾個ImageView或者Layer
思考:功能是實現(xiàn)了,但是性能好像有點(diǎn)受影響。具體原因要看第三方框架的實現(xiàn)原理,當(dāng)然了也有做的很好的。我是個性能控,當(dāng)我拿到這個需求的時候,也嘗試用一些第三方,但結(jié)果不盡人意。最后XWStarView就此產(chǎn)生了。
XWStarView(高性能星星控件)
推薦理由:
- 簡單易用
- 高性能,采用yyLabel異步繪制
- 支持自定義星星樣式,間距
局限性:
- 目前只支持半星,一星評分
- 目前只支持圖片
- 依賴YYLabel
XWStarMaker(外觀配置)
開發(fā)者可以配置間距,最大值,默認(rèn)圖片,選中圖片
@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(核心代碼)
眼尖的同學(xué)已經(jīng)看到了,XWStarView直接繼承了YYLabel,熟悉YYLaebl的開發(fā)者可能知道我要干嘛了。
#import "YYLabel.h" #import "XWStarMaker.h" @class XWStarView; @protocol XWStarViewDelegate <NSObject> @optional -(void)xw_starView:(XWStarView*)tagView star:(NSInteger)star; @end @interface XWStarView : YYLabel @property (nonatomic, assign) NSInteger score; @property (nonatomic,weak) id<XWStarViewDelegate> delegate; -(instancetype)initWithFrame:(CGRect)frame maker:(void (^)(XWStarMaker *))makeBlock; @end
具體的實現(xiàn)細(xì)節(jié)看.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];
//添加點(diǎn)擊事件
__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程序員大概都看得懂代碼吧。實現(xiàn)很簡單,但是效果卻不一般,特別在復(fù)雜列表使用的時候很明顯。
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的愛好者不錯的選擇哦,如果滿足你的業(yè)務(wù)需求,性能方面會讓你很滿意的,不信你就試試(哈哈,調(diào)皮了)。當(dāng)然了蘿卜青菜各有所愛,不喜勿噴。
總結(jié)
程序員的快樂應(yīng)該是每天不斷的學(xué)習(xí),不斷的發(fā)現(xiàn)新東西,讓自己不被拋棄,至少我是那么認(rèn)為的(嘻嘻),你呢?
好了,以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
iOS開發(fā)中UITabBarController的使用示例
這篇文章主要介紹了iOS開發(fā)中UITabBarController的使用示例,代碼基于Objective-C進(jìn)行演示,需要的朋友可以參考下2015-09-09
iOS 通過collectionView實現(xiàn)照片刪除功能
這篇文章主要介紹了iOS 通過collectionView實現(xiàn)照片刪除功能,代碼簡單易懂,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-11-11
safari調(diào)試iOS app web頁面的步驟
這篇文章主要為大家詳細(xì)介紹了safari調(diào)試iOS app web頁面的步驟,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06

