4. 关于一个评分demo实现的注意事项

来源:互联网 发布:北京致远软件 编辑:程序博客网 时间:2024/06/05 04:55

1.层次结构 

#define kFullMark 10


#define kNormalWidth 35

#define kNormalHeight 33


#define kSmallWidth 15

#define kSmallHeight 14


#define kNormalFontSize 30

#define kSmallFontSize 15


#pragma mark - init Method

- (id)initWithFrame:(CGRect)frame

{

    self = [superinitWithFrame:frame];

    if (self) {

        [selfinitGrayStarView];

        [selfinitYellowStarView];      

        [selfinitRatingLabel];     

    }

    return self;

}

2分别实现各个层次

- (void)initGrayStarView //灰色视图

{

    _grayStarsArray = [[NSMutableArrayalloc] initWithCapacity:5];

    for (int index =0; index < 5; index++) {

        UIImageView *imgView = [[UIImageViewalloc] initWithFrame:CGRectZero];

        imgView.image = [UIImageimageNamed:@"gray"];

        [self addSubview:imgView];

        

        [_grayStarsArray addObject:imgView];

    }

}

- (void)initYellowStarView   //黄色视图

{

    baseView = [[UIViewalloc] initWithFrame:CGRectZero];

    baseView.clipsToBounds =YES;//剪切超出的视图

    [selfaddSubview:baseView];

    _yellowStarsArray = [[NSMutableArrayalloc] initWithCapacity:5];

    for (int index =0; index < 5; index++) {

        UIImageView *imgView = [[UIImageViewalloc] initWithFrame:CGRectZero];

        imgView.image = [UIImageimageNamed:@"yellow"];

        [baseView addSubview:imgView];

        [_yellowStarsArray addObject:imgView];

    }

}

- (void)initRatingLabel   //评分标签

{

    _ratingLabel = [[UILabelalloc] initWithFrame:CGRectZero];

    _ratingLabel.textColor = [UIColorredColor];

    _ratingLabel.text = [NSStringstringWithFormat:@"%.1f",self.rating];

    [selfaddSubview:_ratingLabel];

    

}

3重新布局

- (void)layoutSubviews

{

    [superlayoutSubviews];

    float width = 0;

    for (int index =0; index < 5; index ++) {

        UIView *yellowStar = _yellowStarsArray[index];

        UIView *grayStar =  _grayStarsArray[index];

    if (self.style ==kSmallStyle) {

        yellowStar.frame = CGRectMake(0 + width, 0, kSmallWidth, kSmallHeight);//黄色视图

        grayStar.frame   = CGRectMake(0 + width, 0 , kSmallWidth, kSmallHeight);//灰色视图

        width += kSmallWidth;

    }else{

        yellowStar.frame = CGRectMake(0 + width, 0, kNormalWidth, kNormalHeight);

        grayStar.frame   = CGRectMake(0 + width, 0, kNormalWidth, kNormalHeight);

        width += kNormalWidth;

    }

  }

    

    float baseViewWidth = 0;

    baseViewWidth = self.rating/kFullMark*width;

    

    float height = 0;

    if (self.style ==kSmallStyle) {

        baseView.frame =CGRectMake(0,0, baseViewWidth, kSmallHeight);

        _ratingLabel.font = [UIFontboldSystemFontOfSize:kSmallFontSize];//评分字体大小

        height = kSmallHeight;

    }else{

        baseView.frame =CGRectMake(0,0, baseViewWidth, kNormalHeight);

        _ratingLabel.font = [UIFontboldSystemFontOfSize:kNormalFontSize];

        height = kNormalHeight;

    }

    _ratingLabel.frame =CGRectMake(width,0, 0,0);//因为已经设置了字体大小了,这里不需要设置大小

    _ratingLabel.text = [NSStringstringWithFormat:@"%.1f",self.rating];

    [_ratingLabel sizeToFit];//适应大小

    self.frame =CGRectMake(self.frame.origin.x,self.frame.origin.y, width+_ratingLabel.frame.size.width, height);

    }



0 0
原创粉丝点击