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