//把两张图上下重叠,应用UIView和CALayer的基本用法,通过剪裁,实现星级评价效果。
#import "StarView.h"
@implementation StarView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self createStarViewWithStarIndex:3];
}
return self;
}
- (void)createStarViewWithStarIndex:(CGFloat)starIndex
{
//No.1
//开始写代码,现有StarsForeground.png和StarsBackground.png两张图,要求上下重叠实现星级评价效果(即,共五颗星,三星评价时三颗为黄色两颗为灰色,其中黄色五颗星图片为StarsForeground.png)
//图片的宽度
CGFloat imageWidth = 64;
UIImage *image1 = [UIImage imageNamed:@"StarsForeground.png"];
UIImageView *foreView = [[UIImageView alloc] initWithImage: image1];
foreView.frame = CGRectMake(0, 0, imageWidth, 30);
UIImage *image2 = [UIImage imageNamed:@"StarsBackground.png"];
UIImageView *backView = [[UIImageView alloc] initWithImage: image2];
backView.frame = CGRectMake(0, 0, imageWidth, 30);
foreView.clipsToBounds = YES;
foreView.contentMode = UIViewContentModeLeft;
CGFloat w = starIndex * imageWidth / 5;
foreView.frame = CGRectMake(0, 0, w, 30);
[self addSubview:backView];
[self addSubview:foreView];
//end_code
}
@end
iOS星级评价效果
最新推荐文章于 2024-09-05 08:51:02 发布