Due to the needs of the project, you need to design a rating, can display scoring data star rating bar, but there seems to be no such control on iOS, Android has ratingbar this control (and found that an iOS is not as good as Android), it can only be customized, on the Internet to find relevant examples , found are very old version, non-ARC version, and can not be divided into 0 points, there is no indicator effect, so I myself re-wrote a control, named Ratingbar
The first one we do after:
The first step:
Write a Ratingbar child control that inherits from UIView
Step Two:
Declare a ratingbar to modify the rating of the agent, is the score changes to the latest score to tell each other
Step Three:
Declare some methods in the. h file that you want to burst into someone else's call
Fourth Step:
Implement a scoring bar in a. m file
The specific code is as follows:
RatingBar.h File Code
#import <UIKit/UIKit.h>
@class Ratingbar;
/**
* STAR rating Bar Agent
*/
@protocol ratingbardelegate <NSObject>
/**
* Score Changes
*
* @param ratingbar scoring Control
* @param newrating score Value
*/
-(void) Ratingbar: (Ratingbar *) Ratingbar ratingchanged: (float) newrating;
@end
@interface Ratingbar:uiview
/**
* Initialize settings for unselected pictures, half-selected pictures, full-selected images, and agents with changed rating values (you can use
* Block) Implementation
*
* @param deselectedname The picture name is not selected
* @param halfselectedname Half-selected picture name
* @param fullselectedname full Select picture name
* @param delegate Agent
*/
-(void) setimagedeselected: (NSString *) deselectedname halfselected: (NSString *) Halfselectedname fullSelected: ( NSString *) Fullselectedname anddelegate: (id<ratingbardelegate>) delegate;
/**
* Set Rating values
*
* @param rating score Value
*/
-(void) displayrating: (float) rating;
/**
* Get the current score value
*
* @return Score Value
*/
-(float) rating;
/**
* Whether it is an indicator, if it is an indicator, you can not slide, only show the results, not the indicator will be able to slide the modified value
* default is no
*/
@property (nonatomic,assign) BOOL isindicator;
@end
. m file
#import "RatingBar.h"
@interface Ratingbar () {
float starrating;
float lastrating;
float height;
float width;
UIImage *unselectedimage;
UIImage *halfselectedimage;
UIImage *fullselectedimage;
}
@property (Nonatomic,strong) Uiimageview *s1;
@property (Nonatomic,strong) Uiimageview *s2;
@property (Nonatomic,strong) Uiimageview *s3;
@property (Nonatomic,strong) Uiimageview *s4;
@property (Nonatomic,strong) Uiimageview *s5;
@property (Nonatomic,weak) id<ratingbardelegate> delegate;
@end
@implementation Ratingbar
/**
* Initialize settings for unselected pictures, half-selected pictures, full-selected images, and agents with changed rating values (you can use
* Block) Implementation
*
* @param deselectedname The picture name is not selected
* @param halfselectedname Half-selected picture name
* @param fullselectedname Full Select picture name
* @param delegate Agent
*/
-(void) setimagedeselected: (NSString *) deselectedname halfselected: (NSString *) Halfselectedname fullSelected: ( NSString *) Fullselectedname anddelegate: (id<ratingbardelegate>) delegate{
Self.delegate = delegate;
Unselectedimage = [UIImage imagenamed:deselectedname];
Halfselectedimage = Halfselectedname = = Nil? Unselectedimage: [UIImage imagenamed:halfselectedname];
Fullselectedimage = [UIImage imagenamed:fullselectedname];
Height = 0.0,width = 0.0;
if (height < [fullselectedimage size].height) {
Height = [Fullselectedimage size].height;
}
if (height < [halfselectedimage size].height) {
Height = [Halfselectedimage size].height;
}
if (height < [unselectedimage size].height) {
Height = [Unselectedimage size].height;
}
if (width < [fullselectedimage size].width) {
width = [Fullselectedimage size].width;
}
if (width < [halfselectedimage size].width) {
width = [Halfselectedimage size].width;
}
if (width < [unselectedimage size].width) {
width = [Unselectedimage size].width;
}
Control width Adaptation
CGRect frame = [self frame];
CGFloat viewwidth = width * 5;
if (Frame.size.width > Viewwidth) {
Viewwidth = Frame.size.width;
}
Frame.size.width = Viewwidth;
Frame.size.height = height;
[Self setframe:frame];
starrating = 0.0;
lastrating = 0.0;
_S1 = [[Uiimageview alloc] initwithimage:unselectedimage];
_S2 = [[Uiimageview alloc] initwithimage:unselectedimage];
_S3 = [[Uiimageview alloc] initwithimage:unselectedimage];
_S4 = [[Uiimageview alloc] initwithimage:unselectedimage];
_S5 = [[Uiimageview alloc] initwithimage:unselectedimage];
The spacing between star pictures
CGFloat space = (cgfloat) (viewwidth-width*5)/6;
CGFloat StartX = space;
[_s1 setframe:cgrectmake (StartX, 0, width, height)];
StartX + = width + space;
[_s2 setframe:cgrectmake (StartX, 0, width, height)];
StartX + = width + space;
[_s3 setframe:cgrectmake (StartX, 0, width, height)];
StartX + = width + space;
[_s4 setframe:cgrectmake (StartX, 0, width, height)];
StartX + = width + space;
[_s5 setframe:cgrectmake (StartX, 0, width, height)];
[_s1 Setuserinteractionenabled:no];
[_s2 Setuserinteractionenabled:no];
[_s3 Setuserinteractionenabled:no];
[_s4 Setuserinteractionenabled:no];
[_s5 Setuserinteractionenabled:no];
[Self addsubview:_s1];
[Self addsubview:_s2];
[Self ADDSUBVIEW:_S3];
[Self ADDSUBVIEW:_S4];
[Self addsubview:_s5];
}
/**
* Set Rating values
*
* @param rating Evaluation Score
*/
-(void) displayrating: (float) rating{
[_s1 Setimage:unselectedimage];
[_s2 Setimage:unselectedimage];
[_s3 Setimage:unselectedimage];
[_s4 Setimage:unselectedimage];
[_s5 Setimage:unselectedimage];
if (rating >= 0.5) {
[_s1 Setimage:halfselectedimage];
}
if (rating >= 1) {
[_s1 Setimage:fullselectedimage];
}
If (rating >= 1.5) {
[_s2 Setimage:halfselectedimage];
}
if (rating >= 2) {
[_s2 Setimage:fullselectedimage];
}
If (rating >= 2.5) {
[_s3 Setimage:halfselectedimage];
}
if (rating >= 3) {
[_s3 Setimage:fullselectedimage];
}
If (rating >= 3.5) {
[_s4 Setimage:halfselectedimage];
}
if (rating >= 4) {
[_s4 Setimage:fullselectedimage];
}
If (rating >= 4.5) {
[_s5 Setimage:halfselectedimage];
}
if (rating >= 5) {
[_s5 Setimage:fullselectedimage];
}
Starrating = rating;
Lastrating = rating;
[_delegate ratingbar:self ratingchanged:rating];
}
/**
* Get the current score value
*
* @return Score Value
*/
-(float) rating{
return starrating;
}
-(void) Touchesbegan: (Nsset *) touches withevent: (uievent *) event{
[Super Touchesbegan:touches Withevent:event];
}
-(void) touchesended: (Nsset *) touches withevent: (uievent *) event{
[Self touchesrating:touches];
}
-(void) touchesmoved: (Nsset *) touches withevent: (uievent *) event{
[Self touchesrating:touches];
}
Trigger
-(void) touchesrating: (Nsset *) touches{
if (self.isindicator) {
Return
}
Cgpoint point = [[touches anyobject] locationinview:self];
The spacing between star pictures
CGFloat space = (cgfloat) (self.frame.size.width-width*5)/6;
float newrating = 0;
if (point.x >= 0 && point.x <= self.frame.size.width) {
if (point.x <= space+width*0.5f) {
newrating = 0.5;
}else if (Point.x < space*2+width) {
newrating = 1.0;
}else if (Point.x < space*2+width*1.5) {
newrating = 1.5;
}else if (point.x <= 3*space+2*width) {
newrating = 2.0;
}else if (point.x <= 3*space+2.5*width) {
newrating = 2.5;
}else if (point.x <= 4*space+3*width) {
newrating = 3.0;
}else if (point.x <= 4*space+3.5*width) {
newrating = 3.5;
}else if (point.x <= 5*space+4*width) {
newrating = 4.0;
}else if (Point.x <=5*space+4.5*width) {
newrating = 4.5;
}else {
newrating = 5.0;
}
}
if (newrating! = lastrating) {
[Self displayrating:newrating];
}
}
@end
Call:
#import "ViewController.h"
#import "RatingBar.h"
@interface Viewcontroller () <RatingBarDelegate>
@property (Nonatomic,strong) UILabel *mlabel;
@property (Nonatomic,strong) Ratingbar *ratingbar1;
@end
@implementation Viewcontroller
-(void) Viewdidload {
[Super Viewdidload];
[Self setupviews];
}
#pragma mark-Initialize view
-(void) setupviews{
Self.view.backgroundColor = [Uicolor Whitecolor];
RatingBar1
CGFloat width = 200;
CGFloat x = (self.view.bounds.size.width-width) *0.5;
SELF.RATINGBAR1 = [[Ratingbar alloc] Initwithframe:cgrectmake (x, N, Width, 50)];
Add to view
[Self.view ADDSUBVIEW:SELF.RATINGBAR1];
Whether it is an indicator
Self.ratingBar1.isIndicator = NO;
[Self.ratingbar1 setimagedeselected:@ "iconfont-xingunselected" halfselected:@ "iconfont-banxing" fullSelected:@ " Iconfont-xing "Anddelegate:self";
RatingBar2
CGFloat width2 = 250;
CGFloat x2 = (self.view.bounds.size.width-width2) *0.5;
SELF.RATINGBAR2 = [[Ratingbar alloc] Initwithframe:cgrectmake (x2, $, width2, 50)];
Self.ratingBar2.isIndicator = NO;
[Self.ratingbar2 setimagedeselected:@ "iconfont-xingunselected" halfselected:@ "iconfont-banxing" fullSelected:@ " Iconfont-xing "Anddelegate:self";
[Self.view ADDSUBVIEW:SELF.RATINGBAR2];
RatingBar3
CGFloat width3 = 300;
CGFloat x3 = (self.view.bounds.size.width-width3) *0.5;
SELF.RATINGBAR3 = [[Ratingbar alloc] Initwithframe:cgrectmake (x3, Width3, 50)];
Self.ratingBar3.isIndicator = NO;
[Self.ratingbar3 setimagedeselected:@ "iconfont-xingunselected" halfselected:@ "iconfont-banxing" fullSelected:@ " Iconfont-xing "Anddelegate:self";
[Self.view ADDSUBVIEW:SELF.RATINGBAR3];
Uilabel of Display Results
CGFloat LabelX = (self.view.bounds.size.width-400) *0.5f;
UILabel *label = [[UILabel alloc] Initwithframe:cgrectmake (LabelX, 400, 400, 50)];
Label.textalignment = Nstextalignmentcenter;
[Self.view Addsubview:label];
Self.mlabel = label;
}
#pragma mark-ratingbar delegate
-(void) Ratingbar: (Ratingbar *) Ratingbar ratingchanged: (float) newrating{
if (self.ratingbar1 = = Ratingbar) {
Self.mLabel.text = [NSString stringwithformat:@ "The current result of the first scoring strip is:%.1f", newrating];
}else if (self.ratingbar2 = = Ratingbar) {
Self.mLabel.text = [NSString stringwithformat:@ "The current result of the second scoring strip is:%.1f", newrating];
}else if (self.ratingbar3 = = Ratingbar) {
Self.mLabel.text = [NSString stringwithformat:@ "The current result of the third scoring strip is:%.1f", newrating];
}
}
-(void) didreceivememorywarning {
[Super didreceivememorywarning];
Dispose of any resources the can be recreated.
}
@end
ios-a step-by-step way to teach you custom rating star ratingbar--reprinted