123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- //
- // CDPStarEvaluation.m
- // CDPStarEvaluation
- //
- // Created by MAC on 15/4/20.
- // Copyright (c) 2015年 com.xuezi.CDP. All rights reserved.
- //
- #import "CDPStarEvaluation.h"
- @implementation CDPStarEvaluation
- -(id)initWithFrame:(CGRect)frame onTheView:(UIView *)view{
- if (self=[super init]) {
- _commentText=@"请评分";
- //空星级imageView
- _starEmptyImageView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"评价1"]];
- _starEmptyImageView.frame=frame;
- _starEmptyImageView.userInteractionEnabled=YES;
-
- [view addSubview:_starEmptyImageView];
-
- UIImage *image = [self reSizeImage:[UIImage imageNamed:@"评价2"] toSize:CGSizeMake(frame.size.width, frame.size.height)];
- _starImageView=[[UIImageView alloc] initWithImage:image];
- _starImageView.frame=CGRectMake(frame.origin.x,frame.origin.y,0,frame.size.height);
- _starImageView.contentMode=UIViewContentModeLeft;
- _starImageView.clipsToBounds=YES;
- [view addSubview:_starImageView];
- //单击手势
- UITapGestureRecognizer *tapGR=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGR:)];
- [_starEmptyImageView addGestureRecognizer:tapGR];
- //拖动手势
- UIPanGestureRecognizer *panGR=[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGR:)];
- [_starEmptyImageView addGestureRecognizer:panGR];
- }
- return self;
- }
- - (UIImage *)reSizeImage:(UIImage *)image toSize:(CGSize)reSize
- {
- UIGraphicsBeginImageContext(CGSizeMake(reSize.width, reSize.height));
- [image drawInRect:CGRectMake(0, 0, reSize.width, reSize.height)];
- UIImage *reSizeImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
-
- return reSizeImage;
- }
- #pragma mark 外部设置星级
- -(void)setStarWithFloat:(CGFloat)wid
- {
- _starEmptyImageView.userInteractionEnabled = NO;
-
- _starImageView.frame=CGRectMake(_starEmptyImageView.frame.origin.x,_starEmptyImageView.frame.origin.y,wid * _starEmptyImageView.bounds.size.width,_starEmptyImageView.bounds.size.height);
-
- if (wid<=1/5.0) {
- //差
- _commentText=@"差";
- }
- else if(wid<=2/5.0&&wid>1/5.0){
- //一般
- _commentText=@"一般";
- }
- else if(wid<=3/5.0&&wid>2/5.0){
- //好
- _commentText=@"好";
- }
- else if(wid<=4/5.0&&wid>3/5.0){
- //很好
- _commentText=@"很好";
- }
- else{
- //非常好
- _commentText=@"非常好";
- }
- }
- #pragma mark 手势
- //单击手势
- -(void)tapGR:(UITapGestureRecognizer *)tapGR{
- CGPoint tapPoint=[tapGR locationInView:_starEmptyImageView];
- _width = tapPoint.x/_starEmptyImageView.bounds.size.width;
- _starImageView.frame=CGRectMake(_starEmptyImageView.frame.origin.x,_starEmptyImageView.frame.origin.y,tapPoint.x,_starEmptyImageView.bounds.size.height);
-
- if (_width<=1/5.0) {
- //差
- _commentText=@"差";
- }
- else if(_width<=2/5.0&&_width>1/5.0){
- //一般
- _commentText=@"一般";
- }
- else if(_width<=3/5.0&&_width>2/5.0){
- //好
- _commentText=@"好";
- }
- else if(_width<=4/5.0&&_width>3/5.0){
- //很好
- _commentText=@"很好";
- }
- else{
- //非常好
- _commentText=@"非常好";
- }
-
- if (_delegate) {
- [_delegate theCurrentCommentText:_commentText Tag:_delegateTag];
- }
- }
- //拖动手势
- - (void)panGR:(UIPanGestureRecognizer *)panGR
- {
- CGPoint panPoint = [panGR locationInView:_starEmptyImageView];
- if (panPoint.x<0||panPoint.x>_starEmptyImageView.bounds.size.width) {
- return;
- }
- _width=panPoint.x/_starEmptyImageView.bounds.size.width;
-
- _starImageView.frame=CGRectMake(_starEmptyImageView.frame.origin.x,_starEmptyImageView.frame.origin.y,panPoint.x,_starEmptyImageView.bounds.size.height);
-
- if (_width<=1/5.0) {
- //差
- _commentText=@"差";
- }
- else if(_width<=2/5.0&&_width>1/5.0){
- //一般
- _commentText=@"一般";
- }
- else if(_width<=3/5.0&&_width>2/5.0){
- //好
- _commentText=@"好";
- }
- else if(_width<=4/5.0&&_width>3/5.0){
- //很好
- _commentText=@"很好";
- }
- else{
- //非常好
- _commentText=@"非常好";
- }
-
- if (_delegate) {
- [_delegate theCurrentCommentText:_commentText Tag:_delegateTag];
- }
- }
- @end
|