-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStarView.m
99 lines (76 loc) · 2.58 KB
/
StarView.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//
// StarView.m
// BVMovie
//
// Created by wangjin on 15/11/23.
// Copyright (c) 2015年 wangjin. All rights reserved.
//
#import "StarView.h"
@implementation StarView
{
UIView *yellowView;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
//如果通过该方法初始化StarView对象 那么该方法将作为入口方法
-(instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
//创建星星视图
[self createStarView];
}
return self;
}
//如果StarView是被画在xib文件中 则系统默认调用该方法(入口方法)
-(void)awakeFromNib
{
//创建星星视图
[self createStarView];
}
-(void)createStarView
{
//获取灰色与黄色图片对象
UIImage *garyImg = [UIImage imageNamed:@"gray@2x"];
UIImage *yellowImg = [UIImage imageNamed:@"yellow@2x"];
//获取图片宽高
CGFloat width = yellowImg.size.width;
CGFloat hight = yellowImg.size.height;
//创建灰色星星视图
UIView *grayView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width*5, hight)];
//设置灰色星星视图背景图 图片会平铺展示
grayView.backgroundColor = [UIColor colorWithPatternImage:garyImg];
[self addSubview:grayView];
//创建黄色星星视图
yellowView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width*5, hight)];
yellowView.backgroundColor = [UIColor colorWithPatternImage:yellowImg];
[self addSubview:yellowView];
//缩小或者放大星星视图
//获取比例系数
CGFloat scale = self.frame.size.width/yellowView.frame.size.width;
//按比例缩放
grayView.transform = CGAffineTransformMakeScale(scale, scale);
yellowView.transform = CGAffineTransformMakeScale(scale, scale);
//重新设置frame
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)setRatingFinal:(NSNumber *)ratingFinal
{
_ratingFinal = ratingFinal;
CGFloat ratingFinalf = [_ratingFinal floatValue];
if (ratingFinalf<0) {
ratingFinalf = 0;
}
//宽度比例
CGFloat widthScale = ratingFinalf/10.0;
//设置黄图宽度
yellowView.frame = CGRectMake(0, 0, self.frame.size.width*widthScale, self.frame.size.height);
}
@end