码迷,mamicode.com
首页 > 其他好文 > 详细

星星视图

时间:2015-08-27 11:20:43      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:我的文章

方法一:

#import "StarView.h"


@implementation StarView


- (instancetype)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self != nil) {

        [self _initCtreateViews];

    }

    return self;

}

- (void)awakeFromNib

{

    [super awakeFromNib];

    [self _initCtreateViews];

}

//创建视图

- (void)_initCtreateViews

{

    //灰色星星

    UIImage *grayImage = [UIImage imageNamed:@"gray.png"];

    //***星星

    UIImage *yellowImage = [UIImage imageNamed:@"yellow.png"];

    

    //创建灰色星星视图

    _grayStarView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, grayImage.size.width * 5, grayImage.size.height)];

    _grayStarView.backgroundColor = [UIColor colorWithPatternImage:grayImage];

    [self addSubview:_grayStarView];

    

    //创建***星星视图

    _yellowStarView = [[UIView alloc] initWithFrame:_grayStarView.bounds];

    _yellowStarView.backgroundColor = [UIColor colorWithPatternImage:yellowImage];

    [self addSubview:_yellowStarView];

    

    //让当前视图的宽度等于高度的5

    self.width = self.height * 5;

    

    //星星进行缩放

    //计算要缩放的倍数

    CGFloat scale = self.height / grayImage.size.height;

    _grayStarView.transform = CGAffineTransformMakeScale(scale, scale);

    _yellowStarView.transform = CGAffineTransformMakeScale(scale, scale);

   

    //当星星视图修改了transform后,坐标会发生改变,需要重新恢复坐标(transform是以中心点不变进行放缩的)

    _grayStarView.origin = CGPointMake(0, 0);

    _yellowStarView.origin = CGPointZero;//CGPointZero等价于CGPointMake(0, 0)

}


- (void)setRating:(CGFloat)rating

{

    if (_rating != rating) {

        _rating = rating;

        _yellowStarView.width = _rating / 10 * _grayStarView.width;

//        NSLog(@"--------%.2f",_yellowStarView.width);

//        NSLog(@"-----sdfs%.2f",_grayStarView.width);

    }

}

@end

方法二:

    方法一

    UIImageView *starView = (UIImageView *)[self.contentView viewWithTag:202];

    UIImage *image = [UIImage imageNamed:@"gray@2x.png"];

    //修改图片大小

    UIGraphicsBeginImageContext(CGSizeMake(28, 24));

    [image drawInRect:CGRectMake(0, 0, 28, 24)];

    image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    starView.backgroundColor = [UIColor colorWithPatternImage:image];

    

    //获取评分

//    NSString *rating = ratingName.text;

//    CGFloat f = [rating floatValue];

    UIImageView *imgView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,28 * (f / 2), 24)];

    [starView addSubview:imgView1];

    UIImage *image1 = [UIImage imageNamed:@"yellow@2x.png"];

    //修改图片大小

    UIGraphicsBeginImageContext(CGSizeMake(28, 24));

    [image1 drawInRect:CGRectMake(0, 0, 28, 24)];

    image1 = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    imgView1.backgroundColor = [UIColor colorWithPatternImage:image1];


星星视图

标签:我的文章

原文地址:http://10585085.blog.51cto.com/10575085/1688698

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!