设置星级评价

来源:互联网 发布:湖北大数据问责汇报 编辑:程序博客网 时间:2024/05/01 11:21

//

//  StarView.m

//  LimitFree

//

//  Created by mac on 15-1-6.

//  Copyright (c) 2015 LaoWen. All rights reserved.

//


#import "StarView.h"


//StarView这个类是UIView的子类,要想在Xib上使用这个类,可以在xib上放一个UIView,然后设置UIViewCustom ClassStarView(方法跟设置xibCellCustom Class相同),这样当xib被使用时系统就会创建StarView这个类的对象。


@implementation StarView

{

   UIImageView *_backgroundImageView;//显示背景图片

   UIImageView *_foregroundImageView;//显示前景图片

}


- (id)initWithFrame:(CGRect)frame

{

   self = [superinitWithFrame:frame];

   if (self) {

        // Initialization code

        [selfcustomUI];

    }

    return self;

}


//放在Xib中的控件其初始化方法为initWithCoder

- (id)initWithCoder:(NSCoder *)aDecoder

{

   self = [superinitWithCoder:aDecoder];

   if (self) {

        [selfcustomUI];

    }

    return self;

}


- (void)customUI

{

   UIImage *backgroundImage = [UIImageimageNamed:@"StarsBackground"];

   _backgroundImageView = [[UIImageViewalloc]initWithImage:backgroundImage];

   _backgroundImageView.frame =CGRectMake(0,0, backgroundImage.size.width, backgroundImage.size.height);

    [selfaddSubview:_backgroundImageView];

    

   UIImage *foregroundImage = [UIImageimageNamed:@"StarsForeground"];

   _foregroundImageView = [[UIImageViewalloc]initWithImage:foregroundImage];

   _foregroundImageView.frame =CGRectMake(0,0, foregroundImage.size.width, foregroundImage.size.height);

    [selfaddSubview:_foregroundImageView];

}


//设置显示几颗星

- (void)setStarCount:(float) starCount

{

   float width = starCount/5*_backgroundImageView.frame.size.width;

    CGRect frame =_foregroundImageView.frame;

    frame.size.width = width;

    _foregroundImageView.frame = frame;

    _foregroundImageView.contentMode =UIViewContentModeLeft;//UIImageView默认情况下图片会自动缩放适应UIImageView的大小,设置contentMode属性后情况就会发生变化。设置成UIViewContentModeLeft表示图片的左边与UIImageView的左边对齐,配合clipsToBounds属性可以实现图片的剪切效果

    _foregroundImageView.clipsToBounds =YES;//当图片超出UIImageView的范围时是否剪切。

}


- (void)dealloc

{

    [_foregroundImageViewrelease];

    [_backgroundImageViewrelease];

    [superdealloc];

}


0 0
原创粉丝点击