实现电影评分的星级显示效果
核心代码:
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor grayColor];
//电影评分
float score = 6.8; //十分制
//星星的图片
UIImage *yellowStarImage = [UIImage imageNamed:@"yellow"];
UIImage *grayStarImage = [UIImage imageNamed:@"gray"];
float num = score/10*5; //十分制转换为5星制度
//两个view的位置要相同,黄色星星宽度由num来决定
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(100, 100, num*(yellowStarImage.size.width), yellowStarImage.size.height)];
UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 5*(grayStarImage.size.width), grayStarImage.size.height)];
view1.backgroundColor = [UIColor colorWithPatternImage:yellowStarImage];
view2.backgroundColor = [UIColor colorWithPatternImage:grayStarImage];
//注意添加subView顺序,先添加灰色星星背景图,再添加黄色星星背景图
[self.view addSubview:view2];
[self.view addSubview:view1];
// Do any additional setup after loading the view, typically from a nib.
}
效果图: