//将背景图加入superview
UIImageView *thermometerBackground = [[UIImageView alloc] initWithFrame:THERMOMETER_FRAME];
[thermometerBackground setImage:[UIImage imageNamed:@
"ThermometerBackground.png"
]];
[self.view addSubview:self.thermometerBackground];
//将溶液图加入背景图
UIImageView *thermometer = [[UIImageView alloc]init];
[self.thermometerBackground addSubview:self.thermometer];
//将刻度图加入背景图
UIImageView *thermometerCalibration = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@
"ThermometerCalibration.png"
]];
[self.thermometerCalibration setFrame:CGRectMake(
0
,
10
, thermometerBackground.frame.size.width, thermometerCalibration.image.size.height*thermometerBackground.frame.size.width/thermometerCalibration.frame.size.width)];
[self.thermometerBackground addSubview:thermometerCalibration];
然后,根据度数生成对应高度的image;
UIImage* image = [UIImage imageNamed:@
"Thermometer.png"
];
UIEdgeInsets insets = UIEdgeInsetsMake(
20
,
0
,
25
,
0
);
image = [image resizableImageWithCapInsets:insets];
int
top =
10.00
+(
38.00
-temperature)*
20.00
;
[self.thermometer setFrame:CGRectMake(
0
, top, self.thermometerBackground.frame.size.width, self.thermometerBackground.frame.size.height-top)];
[self.thermometer setImage:image];
- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:
这个函数是UIImage的一个实例函数,它的功能是创建一个内容可拉伸,而边角不拉伸的图片,需要两个参数,第一个是左边不拉伸区域的宽度,第二个参数是上面不拉伸的高度。根据设置的宽度和高度,将接下来的一个像素进行左右扩展和上下拉伸。