Implementing high-performance rating controls in iOS
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:
- Master writes a general space himself (when time is sufficient)
- Find a better third party online (when time is tight)
- More directly, put a few ImageView or Layer directly
##High performance, using yyLabel asynchronous drawing
- Supports custom star style, spacing
- ##Limitations Sex:
- Currently only supports half-star rating, one-star rating
- Currently only supports pictures
- XWStarMaker (Appearance Configuration)
- Developers can configure the spacing, maximum value, default image, and selected image
@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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











How to use Swoole to implement a high-performance HTTP reverse proxy server Swoole is a high-performance, asynchronous, and concurrent network communication framework based on the PHP language. It provides a series of network functions and can be used to implement HTTP servers, WebSocket servers, etc. In this article, we will introduce how to use Swoole to implement a high-performance HTTP reverse proxy server and provide specific code examples. Environment configuration First, we need to install the Swoole extension on the server

PHP and WebSocket: Building high-performance real-time applications As the Internet develops and user needs increase, real-time applications are becoming more and more common. The traditional HTTP protocol has some limitations when processing real-time data, such as the need for frequent polling or long polling to obtain the latest data. To solve this problem, WebSocket came into being. WebSocket is an advanced communication protocol that provides two-way communication capabilities, allowing real-time sending and receiving between the browser and the server.

C++ is a high-performance programming language that provides developers with flexibility and scalability. Especially in large-scale data processing scenarios, the efficiency and fast computing speed of C++ are very important. This article will introduce some techniques for optimizing C++ code to cope with large-scale data processing needs. Using STL containers instead of traditional arrays In C++ programming, arrays are one of the commonly used data structures. However, in large-scale data processing, using STL containers, such as vector, deque, list, set, etc., can be more

With the continuous development of science and technology, speech recognition technology has also made great progress and application. Speech recognition applications are widely used in voice assistants, smart speakers, virtual reality and other fields, providing people with a more convenient and intelligent way of interaction. How to implement high-performance speech recognition applications has become a question worth exploring. In recent years, Go language, as a high-performance programming language, has attracted much attention in the development of speech recognition applications. The Go language has the characteristics of high concurrency, concise writing, and fast execution speed. It is very suitable for building high-performance

Use Go language to develop high-performance face recognition applications Abstract: Face recognition technology is a very popular application field in today's Internet era. This article introduces the steps and processes for developing high-performance face recognition applications using Go language. By using the concurrency, high performance, and ease-of-use features of the Go language, developers can more easily build high-performance face recognition applications. Introduction: In today's information society, face recognition technology is widely used in security monitoring, face payment, face unlocking and other fields. With the rapid development of the Internet

Apps in Microsoft Teams extend Teams functionality on mobile and desktop. Microsoft Teams applications are similar to web-based SaaS applications, do not need to be installed locally, and can only work within the allowed scope. There are more than 2,000 apps in the Microsoft Teams store, some of which are very popular. Collaboration apps from Adobe, Atlassian and Workday have more than 10,000 monthly active users on Teams. Yesterday, Microsoft announced that it is introducing ratings and reviews to the Microsoft Teams app. Not only does this help users choose apps that suit their purposes, but it also allows developers

Technical practice of Docker and SpringBoot: quickly build high-performance application services Introduction: In today's information age, the development and deployment of Internet applications have become increasingly important. With the rapid development of cloud computing and virtualization technology, Docker, as a lightweight container technology, has received widespread attention and application. SpringBoot has also been widely recognized as a framework for rapid development and deployment of Java applications. This article will explore how to combine Docker and SpringB

1. How to rate Douban? Douban effective rating tutorial! 1. Open the Douban app downloaded and installed from this site, and click on the menu at the bottom of the homepage to select books and videos. 2. Enter the work we want to evaluate in the search box. 3. After entering the work details page, click on the rating box. 4. After jumping to the page, give a rating according to your preferences and click the OK button. 5. Finally, the scoring success page pops up to remind us that the scoring is successful.
