Home Web Front-end H5 Tutorial Implementing high-performance rating controls in iOS

Implementing high-performance rating controls in iOS

Mar 26, 2018 pm 02:05 PM
control score high performance

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

Thinking: The function is implemented, but the performance seems to be affected a bit. The specific reason depends on the implementation principle of the third-party framework. Of course, there are also some that do very well. I am a performance control person. When I got this requirement, I also tried to use some third parties, but the results were not satisfactory. Finally, XWStarView was born.

##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

Depends on YYLabel
  • 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
Copy after login
  • XWStarView.m (core code)

  • Sharp-eyed students have already seen that XWStarView directly inherits YYLabel. Developers who are familiar with YYLaebl may know what I am going to do.

    #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
    Copy after login
    For specific implementation details, see the .m file
    @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
    Copy after login
    As long as you are an iOS programmer, you can probably understand the code. The implementation is simple, but the effect is extraordinary, especially when using complex lists.

    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;
    Copy after login
    XWStarView is a good choice for YYLabel enthusiasts. If it meets your business needs, the performance will make you happy Very satisfied, just try it if you don’t believe me (haha, that’s naughty). Of course, everyone has their own preferences for radishes and greens, so don’t criticize them if you don’t like them.

    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!

    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

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

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    Hot Topics

    Java Tutorial
    1654
    14
    PHP Tutorial
    1252
    29
    C# Tutorial
    1225
    24
    How to use Swoole to implement a high-performance HTTP reverse proxy server How to use Swoole to implement a high-performance HTTP reverse proxy server Nov 07, 2023 am 08:18 AM

    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 PHP and WebSocket: Building high-performance, real-time applications Dec 17, 2023 pm 12:58 PM

    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++ High-Performance Programming Tips: Optimizing Code for Large-Scale Data Processing C++ High-Performance Programming Tips: Optimizing Code for Large-Scale Data Processing Nov 27, 2023 am 08:29 AM

    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

    Use Go language to develop and implement high-performance speech recognition applications Use Go language to develop and implement high-performance speech recognition applications Nov 20, 2023 am 08:11 AM

    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 Use Go language to develop high-performance face recognition applications Nov 20, 2023 am 09:48 AM

    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

    Microsoft introduces app ratings and reviews for Microsoft Teams apps Microsoft introduces app ratings and reviews for Microsoft Teams apps Nov 08, 2023 pm 11:21 PM

    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 Spring Boot: quickly build high-performance application services Technical practice of Docker and Spring Boot: quickly build high-performance application services Oct 21, 2023 am 08:18 AM

    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

    How do you rate Douban? Douban effective rating tutorial! How do you rate Douban? Douban effective rating tutorial! Mar 16, 2024 am 08:31 AM

    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.

    See all articles