1. 程式人生 > >iOS-68-星星評價、顯示小數點星星評價效果(附demo)

iOS-68-星星評價、顯示小數點星星評價效果(附demo)

1、上效果圖:
這裡寫圖片描述

2、
第一個是顯示的7.2分的評分
第二個可以點選選擇評分

3、主要程式碼:

- (void)creatStarView{
    UIImage *gray   = [UIImage imageNamed:@"starGrey"];
    UIImage *yellow = [UIImage imageNamed:@"strayellow"];

    //1.獲取星星檢視的初始寬高
    CGFloat width  = gray.size.width *5;
    CGFloat height = gray.size.height;

    //2.初始化星星檢視
    _grayView = [[UIView alloc] initWithFrame:CGRectMake(0
, 0, width, height)]; [_grayView setBackgroundColor:[UIColor colorWithPatternImage:gray]]; // 平鋪 [self addSubview:_grayView]; _yellowView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, height)]; [_yellowView setBackgroundColor:[UIColor colorWithPatternImage:yellow]]; // 平鋪 [self addSubview:_yellowView];
//3.放大星星檢視與self等高等寬 CGFloat scaleW = self.frame.size.width/width; CGFloat scaleH = self.frame.size.height/height; _grayView.transform = CGAffineTransformMakeScale(scaleW, scaleH); _yellowView.transform = CGAffineTransformMakeScale(scaleW, scaleH); //4.重置星星檢視座標 _grayView.frame
= CGRectMake(0, 0, self.frame.size.width, self.frame.size.height); _yellowView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height); } -(void)setRating:(float)rating{ // _rating = rating; _yellowView.frame = CGRectMake(0, 0, self.frame.size.width *rating/10, self.frame.size.height); }