星級(jí)評(píng)分顯示

首先創(chuàng)建一個(gè)StarView
#import "StarView.h"

@implementation StarView
{
    UIImageView * _foregroundImageView;// 前景圖
    UIImageView * _backgroundImageView; // 背景圖
}

// 創(chuàng)建視圖
- (void)createViews
{
    _backgroundImageView = [[UIImageView alloc] init];
    [self addSubview:_backgroundImageView];
    // 自動(dòng)布局
    _backgroundImageView.translatesAutoresizingMaskIntoConstraints = NO;
    [_backgroundImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self);
        make.top.equalTo(self);
        make.width.equalTo(@46);
        make.height.equalTo(@12);
    }];
    // 設(shè)置背景圖屬性
    _backgroundImageView.image = [UIImage imageNamed:@"star_gray"];
    // 設(shè)置圖片顯示模式
    _backgroundImageView.contentMode = UIViewContentModeLeft;
    
    // 前景圖
    _foregroundImageView = [[UIImageView alloc]
                            initWithImage:[UIImage imageNamed:@"star_hight"]];
    [self addSubview:_foregroundImageView];
    [_foregroundImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(_backgroundImageView);
    }];
    // 設(shè)置圖片顯示模式
    _foregroundImageView.contentMode = UIViewContentModeLeft;
    // 裁剪
    _foregroundImageView.clipsToBounds = YES;
}

// 重寫(xiě)init方法
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self createViews];
    }
    return self;
}

// 當(dāng)在Xib或者Storyboard中關(guān)聯(lián)類(lèi)時(shí),程序從xib或者storyboard創(chuàng)建對(duì)象時(shí),會(huì)調(diào)用該方法
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder]) {
        [self createViews];
    }
    return self;
}
-(void)setStarValue:(CGFloat)starValue
{
    _starValue = starValue;
    if (_starValue >= 0 && _starValue <= 5)
    {
        // 重建約束
        [_foregroundImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_backgroundImageView);
            make.top.equalTo(_backgroundImageView);
            make.height.equalTo(_backgroundImageView);
            make.width.equalTo(_backgroundImageView).multipliedBy(_starValue/5);
        }];
    }
    
//    NSLog(@"starValue is %0.1f",_starValue/5);
}

#import <UIKit/UIKit.h>

@interface StarView : UIView

@property (nonatomic, assign) CGFloat starValue; // 星標(biāo)的值
@end
最后在要顯示星級(jí)評(píng)價(jià)的界面將其帶入
/**星級(jí)*/
    // 設(shè)置星標(biāo)的值
    self.calenderStartView.starValue =[_calendarModel.star floatValue];

最后來(lái)一張顯示圖

IMG_1688.PNG
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,269評(píng)論 25 708
  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,923評(píng)論 18 139
  • 蒹葭essay閱讀 157評(píng)論 0 0
  • 生活多一些讓你看到就可以會(huì)心一笑的小物的感覺(jué)非常棒,讓生活變得非常可愛(ài),大于號(hào)給大家準(zhǔn)備了很多萌趣有意思的小東西,...
    大于號(hào)閱讀 413評(píng)論 0 2
  • 年關(guān)將至無(wú)人顧, 幽幽退居入空谷。 夢(mèng)里重游故鄉(xiāng)路, 誰(shuí)道阿斗不思蜀。
    dalianmaoliang閱讀 255評(píng)論 0 0