1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//
// StarBar.m
// redstar
//
// Created by admin on 15/11/6.
// Copyright © 2015年 ZWF. All rights reserved.
//
#import "StarBar.h"
#define ZOOM 0.7f
@interface StarBar ()
@property (nonatomic,strong) UIView *bottomView;
@property (nonatomic,strong) UIView *topView;
@property (nonatomic,assign) CGFloat starWidth;
@end
@implementation StarBar
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor whiteColor];
self.userInteractionEnabled = YES;
self.bottomView = [[UIView alloc] initWithFrame:self.bounds];
self.bottomView.userInteractionEnabled = NO;
[self addSubview:self.bottomView];
self.topView = [[UIView alloc] initWithFrame:CGRectZero];
self.topView.clipsToBounds = YES;
self.topView.userInteractionEnabled = NO;
[self addSubview:self.topView];
// 添加手势
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[self addGestureRecognizer:tap];
[self addGestureRecognizer:pan];
CGFloat width = frame.size.width/7.0;
self.starWidth = width;
for(int i = 0;i<5;i++){
UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.height * ZOOM, frame.size.height * ZOOM)];
img.center = CGPointMake((i+1.5)*width, frame.size.height/2);
img.image = [UIImage imageNamed:@"star_before"];
[self.bottomView addSubview:img];
UIImageView *img2 = [[UIImageView alloc] initWithFrame:img.frame];
img2.center = img.center;
img2.image = [UIImage imageNamed:@"star_after"];
[self.topView addSubview:img2];
}
self.enable = YES;
}
return self;
}
- (void)setStarNumber:(NSInteger)starNumber
{
if (_starNumber != starNumber) {
_starNumber = starNumber;
self.topView.frame = CGRectMake(0, 0, self.starWidth*(starNumber+1), self.bounds.size.height);
}
}
- (void)tap:(UITapGestureRecognizer *)gesture
{
if (self.enable) {
CGPoint point = [gesture locationInView:self];
NSInteger count = (int)(point.x / self.starWidth)+1;
self.topView.frame = CGRectMake(0, 0, self.starWidth * count, self.bounds.size.height);
if (count > 5) {
_starNumber = 5;
} else {
_starNumber = count - 1;
}
}
}
- (void)pan:(UIPanGestureRecognizer *)gesture
{
if (self.enable) {
CGPoint point = [gesture locationInView:self];
NSInteger count = (int)(point.x/self.starWidth);
if (count >= 0 && count <= 5 && _starNumber != count) {
self.topView.frame = CGRectMake(0, 0, self.starWidth * (count + 1), self.bounds.size.height);
_starNumber = count;
}
}
}
@end