This time I will bring you a high-performance scoring control in iOS. What are the precautions for implementing a high-performance scoring control in iOS? The following is a practical case, let’s take a look.
Preface
As experienced drivers, have you ever encountered such a demand? Each product or merchant item has a star rating or other rating, which looks like the following rendering
##Implementation plan:
##High performance, using yyLabel asynchronous drawing
@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 (core code)
#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
@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
XWStarView use
_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;
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website! Recommended reading:
Detailed explanation of H5’s storage method
How to use H5’s constraint verification API
The above is the detailed content of Implementing high-performance rating controls in iOS. For more information, please follow other related articles on the PHP Chinese website!