IOS Scoring (evaluation) Star Chart scoring function _ios

Source: Internet
Author: User
Tags reserved uikit

Download Address: Https://github.com/littleSunZheng/StarGradeView

Cause: The project often involves the user's score feedback, in my "E Chinese Medicine" project, involves several places. I have referred to the United States and drop the score map.

The scoring view is divided into two kinds: display and scoring.

(1) In most cases "scoring function" is to introduce Easy-to-use. That kind of star accurately displays the percentage (score) function is not good to use, this most uses in the display score, does not need the user to click, because the user wants to evaluate "9.8 points", the finger is not accurate clicks. But when you show it, you can show it perfectly according to the data. The realization principle is two pictures, one is "the Gray" the star Five, one is "the gold" the star Five. Let the ImageView mode be set (unwanted photos are not displayed). According to the proportion of the upper Golden Star ImageView the length of the adjustment, the proportion of the stars will naturally show good.

(2) User operation rating of the Star view: I do here is the scoring. The principle of implementation is simple, when you operate the functions of other software can be combined to think of gestures.

On Source:

StarGradeView.h//Ecmcustomer////Created by Zhengpeng on 2016/11/4. Copyright©2016 year Zhang Jin.
All rights reserved. #import <UIKit/UIKit.h> @protocol stargradeviewdelegate <NSObject>-(void) Didselectedindex: (NSString *
) index;
@end @interface Stargradeview:uiview @property (nonatomic, assign) ID <StarGradeViewDelegate> delegate;
View frame and want to have a few stars (depending on the design 5 common or 10)-(Instancetype) initWithFrame: (CGRect) frame Withtnumberofpart: (nsinteger) num;
@end////STARGRADEVIEW.M//Ecmcustomer///Created by Zhengpeng on 2016/11/4. Copyright©2016 year Zhang Jin.
All rights reserved.
#import "StarGradeView.h" @interface Stargradeview () {UIView *_btnview;//the background of the Stars view UIView *_shouview;//The background view of the stars CGFloat _height;//The number of high Nsinteger _btnnum;//buttons Nsinteger _index;//the first few} @end @implementation Stargradeview-(Instancet ype) initWithFrame: (CGRect) frame Withtnumberofpart: (nsinteger) num{self = [super Initwithframe:frame]; _height =
Frame.size.height;
_btnnum = num; CGFloat SELFW = FramE.size.width;
CGFloat starw = frame.size.height;
_btnview = [[UIView alloc] Initwithframe:cgrectmake ((selfw-starw*num)/2, 0, Starw*num, STARW)]; for (int i = 0; i< num; i++) {UIButton *starbtn = [UIButton buttonwithtype:uibuttontypecustom]; starbtn.frame = CGRect
Make (STARW * I, 0, STARW, STARW);
[Starbtn setimage:[uiimage imagenamed:@ "Star_off"] forstate:uicontrolstatenormal];
[Starbtn setimage:[uiimage imagenamed:@ "star_on"] forstate:uicontrolstateselected];
Starbtn.tag = 1991+i;
[Starbtn Setadjustsimagewhenhighlighted:no];
[_btnview ADDSUBVIEW:STARBTN];
} _shouview = [[UIView alloc] Initwithframe:cgrectmake (0, 0, Starw*num, STARW)];
[_btnview Addsubview:_shouview];
[Self addsubview:_btnview];
return self;
}//sliding required. -(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (uievent *) event{cgpoint point = [self gettouchpoint:
Touches];
int j = (int) (point.x/_height);
_index = j; for (Nsinteger i = 0; i < _btnnum i++) {if (i<=j) {UIButton *btn = [_btnview viewwITHTAG:I+1991];
btn.selected = YES;
}else{UIButton *btn = [_btnview viewwithtag:i+1991]; btn.selected = NO;}}
}//sliding required. -(void) touchesmoved: (Nsset<uitouch *> *) touches withevent: (uievent *) event{cgpoint point = [self gettouchpoint:
Touches];
int j = (int) (point.x/_height);
_index = j; for (Nsinteger i = 0; i < _btnnum; i++) {if (i<=j) {UIButton *btn = [_btnview viewwithtag:i+1991]; btn.selected =
YES;
}else{UIButton *btn = [_btnview viewwithtag:i+1991]; btn.selected = NO;}}
}//sliding required. -(void) touchesended: (Nsset<uitouch *> *) touches withevent: (uievent *) event{if ([self.delegate Respondstoselector: @selector (didselectedindex:)]) {[self.delegate didselectedindex:[nsstring stringwithformat:@ '%
LD ", _index+1]]; }//Take the position of the gesture on the screen point-(Cgpoint) Gettouchpoint: (Nsset<uitouch *>*) touches{Uitouch *touch = [touches anyobject]
;
Cgpoint point = [Touch Locationinview:_shouview];
return point; //If the range clicked is in the area of the button-(BOOL) POINTINBTN: (UIButton *) btn Withpoint: (CGPoint) point{if (Cgrectcontainspoint (Btn.frame, point) {return YES;}
else{return No.} return nil;
/*//Only override drawrect:if you perform custom drawing.
An empty implementation adversely affects performance during. -(void) DrawRect: (cgrect) Rect {//Drawing code} */@end

When used:

Stargradeview *view = [[Stargradeview alloc] Initwithframe:cgrectmake (0, 375,) withtnumberofpart:5];
View.delegate = self;
[Self.view Addsubview:view];
and implement the Proxy method
-(void) Didselectedindex: (NSString *) index{
NSLog (@ "%@", index);
}

Note: Here Chettu attention: As long as a star, and the request is a square star picture has left white. Look at the code to see why you want to cut the diagram. 1 is beautiful 2 is easy to calculate.

The above is a small set to introduce the iOS score (evaluation) star Map scoring function, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.