星星的模块封装类 IDSStarsScoreView
1 IDSStarsScoreView 的实现效果
2 类的封装方法:
<声明文件>
//
// IDSStarsScoreView.h
// Near
//
// Created by levi.duan on 2017/10/23.
// Copyright © 2017年 Near. All rights reserved.
//
#import <UIKit/UIKit.h>
@classIDSStarsScoreView;
@protocol IDSStarsScoreViewDelegate<NSObject>
/**
评分视图点击回调
@param starsView 评分视图
@param score 所选分数
*/
- (void)startsScoreView:(IDSStarsScoreView *)starsView didSelectedScore:(CGFloat)score;
@end
@interface IDSStarsScoreView : UIView
// 星星是否可以点击
@property (nonatomic, assign) BOOL clickTheStar;
@property (nonatomic, weak) id<IDSStarsScoreViewDelegate> delegate;
/*
* size :图像size
* margin :两个星星的间距
* score :显示的星星数
*/
- (instancetype)initWithStarSize:(CGSize)size margin:(CGFloat)margin score:(CGFloat)starNum;
/*
* starNum :显示的星星数
*/
- (void)startInitWithScore:(CGFloat)starNum;
/*
* 获取当前视图评分
*/
- (CGFloat)getCurrentSocore;
@end
<实现文件>
//
// IDSStarsScoreView.m
// Near
//
// Created by levi.duan on 2017/10/23.
// Copyright © 2017年 Near. All rights reserved.
//
#import "IDSStarsScoreView.h"
@interfaceIDSStarsScoreView ()
@property (nonatomic, strong) NSMutableArray *starImageViewArray;
@property (nonatomic, assign) CGFloat stores;
@end
@implementation IDSStarsScoreView
- (instancetype)initWithStarSize:(CGSize)size margin:(CGFloat)margin score:(CGFloat)starNum
{
CGRect frame = CGRectMake(0, 0, size.width*5 + margin*4, size.height);
_stores = starNum;
_clickTheStar = NO;
if (self = [superinitWithFrame:frame]) {
[selfinitWithViewWithTheSize:size margin:margin];
}
returnself;
}
- (void)initWithViewWithTheSize:(CGSize)starSize margin:(CGFloat)starMargin
{
_starImageViewArray = [[NSMutableArrayalloc] init];
UIImageView *imageView;
CGFloat starNum = _stores;
for (int i=0; i<5; ++i) {
if (starNum > 0.5) {
imageView = [[UIImageViewalloc] initWithImage:[UIImageimageNamed:@"ic_yiqiwan_star1"]];
}
elseif (starNum > 0){
imageView = [[UIImageViewalloc] initWithImage:[UIImageimageNamed:@"ic_yiqiwan_star3"]];
}
else {
imageView = [[UIImageViewalloc] initWithImage:[UIImageimageNamed:@"ic_yiqiwan_star2"]];
}
imageView.frame = CGRectMake(self.bounds.origin.x+(i*starSize.width+i*starMargin), 0, starSize.width, starSize.height);
[selfaddSubview:imageView];
[_starImageViewArrayaddObject:imageView];
--starNum;
}
}
- (void)startInitWithScore:(CGFloat)starNum
{
_stores = starNum;
UIImageView *imageView;
for (int i=0; i<5; ++i) {
imageView = _starImageViewArray[i];
if (starNum > 0.5) {
imageView.image = [UIImageimageNamed:@"ic_yiqiwan_star1"];
}
elseif (starNum > 0){
imageView.image = [UIImageimageNamed:@"ic_yiqiwan_star3"];
}
else {
imageView.image = [UIImageimageNamed:@"ic_yiqiwan_star2"];
}
--starNum;
}
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if (!_clickTheStar) {
return;
}
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self];
UIImageView *imageView ;
_stores = 0;
for(int i=0; i<5; ++i){
imageView = _starImageViewArray[i];
if ((touchPoint.x > 0) && (touchPoint.x < self.bounds.size.width) && (touchPoint.y > 0) && (touchPoint.y < self.bounds.size.height)) {
if (imageView.frame.origin.x <= touchPoint.x) {
++_stores;
imageView.image = [UIImageimageNamed:@"ic_yiqiwan_star1"];
}
else{
imageView.image = [UIImageimageNamed:@"ic_yiqiwan_star2"];
}
}
}
if ([self.delegaterespondsToSelector:@selector(startsScoreView:didSelectedScore:)]) {
[self.delegatestartsScoreView:selfdidSelectedScore:_stores];
}
}
- (CGFloat)getCurrentSocore
{
return_stores;
}
@end
声明方法如下:
- (IDSStarsScoreView *)starsView
{
if (!_starsView) {
if (IS_IPHONE_5 || IS_IPHONE_4_OR_LESS) {
_starsView = [[IDSStarsScoreViewalloc] initWithStarSize:CGSizeMake(12,12) margin:0.5score:4.0];
}
else {
_starsView = [[IDSStarsScoreViewalloc] initWithStarSize:CGSizeMake(13,13) margin:2score:4.0];
}
_starsView.frame = CGRectMake(CGRectGetMinX(self.scoreLabel.frame)-_starsView.frame.size.width-3, 0, _starsView.frame.size.width, _starsView.frame.size.height);
_starsView.centerY = self.scoreLabel.centerY;
}
return_starsView;
}
数据联调用法如下:
if (!IS_NS_STRING_EMPTY(model.gameInfo.score)) {
_scoreLabel.text = model.gameInfo.score;
[_scoreLabel sizeToFit];
_scoreLabel.frame = CGRectMake(self.bounds.size.width-10-_scoreLabel.contentSize.width,99, _scoreLabel.contentSize.width, _scoreLabel.contentSize.height);
_scoreLabel.centerY = self.soundImageView.centerY;
[_starsView startInitWithScore:model.gameInfo.score.floatValue];
}
- OVER