-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathYXLiveMillionAnswerButton.m
176 lines (148 loc) · 4.9 KB
/
YXLiveMillionAnswerButton.m
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//
// YXLiveMillionAnswerButton.m
// YXLiveKit
//
// Created by yangzhaokun on 2018/1/13.
// Copyright © 2018年 YIXIA. All rights reserved.
//
#import "YXLiveMillionAnswerButton.h"
#import "UIColor+YXTColor.h"
@interface YXLiveMillionAnswerButton ()
@property (nonatomic, strong) CAShapeLayer *progressLayer; //进度条
@property (nonatomic, strong) CADisplayLink *displayLink;
@property (nonatomic, assign) CGFloat animationProgress;
@property (nonatomic, assign) CGFloat progress;
@end
@implementation YXLiveMillionAnswerButton
+ (instancetype)buttonWithType:(UIButtonType)buttonType
{
YXLiveMillionAnswerButton *button = [super buttonWithType:buttonType];
if (button) {
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
button.contentEdgeInsets = UIEdgeInsetsMake(0,20, 0, 0);
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button.titleLabel setFont:[UIFont systemFontOfSize:14]];
}
return button;
}
- (void)setFrame:(CGRect)frame
{
[super setFrame:frame];
// // 加载图片
// UIImage *image = [UIImage imageNamed:@"million_btn_rect"];
//
// // 设置左边端盖宽度
// NSInteger leftCapWidth = image.size.width * 0.5;
// // 设置上边端盖高度
// NSInteger topCapHeight = image.size.height * 0.5;
//
// UIImage *newImage = [image stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:topCapHeight];
//
//
// [self setBackgroundImage:newImage forState:UIControlStateNormal];
self.layer.masksToBounds = YES;
self.layer.cornerRadius = frame.size.height/2;
self.layer.borderColor = [UIColor yxt_colorWithHex:@"DDDDDD"].CGColor;
self.layer.borderWidth = 0.6f;
// _progressColor = [UIColor colorWithRed:66/255. green:204/255. blue:118/255. alpha:1];
self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(updateUI)];
self.displayLink.paused = YES;
[self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
}
- (void)updateUI
{
_animationProgress += 1/60.f;
if (_animationProgress > _progress) {
_animationProgress = _progress;
_displayLink.paused = YES;
}
[self setProgress:_animationProgress];
}
- (void)setNum:(NSInteger)num andProgress:(CGFloat)progress
{
if (progress > 0 && progress < 0.01) {
progress = 0.01;
}
[self addSubview:self.numLabel];
self.numLabel.hidden = NO;
[self.numLabel setText:[NSString stringWithFormat:@"%ld",(long)num]];
[self.layer insertSublayer:self.progressLayer atIndex:0];
_animationProgress = 0;
_progress = progress;
self.displayLink.paused = NO;
}
- (UILabel *)numLabel
{
if (!_numLabel) {
_numLabel = [[UILabel alloc]initWithFrame:CGRectMake(0.f, CGRectGetHeight(self.frame)/2 - 10, CGRectGetWidth(self.frame) - 20, 20)];
_numLabel.font = [UIFont systemFontOfSize:14];
_numLabel.textAlignment = NSTextAlignmentRight;
_numLabel.textColor = [UIColor blackColor];
}
return _numLabel;
}
- (void)setProgress:(CGFloat)progress
{
progress = MIN(MAX(progress, 0.0), 1.0);
dispatch_async(dispatch_get_main_queue(), ^{
self.progressLayer.path = [self progressPathWithProgress:progress].CGPath;
});
}
- (void)resetButton
{
if (self.progressLayer.superlayer) {
[self.progressLayer removeFromSuperlayer];
}
self.backgroundColor = [UIColor clearColor];
}
- (void)setProgressColor:(UIColor *)progressColor
{
_progressColor = progressColor;
if (_progressLayer) {
_progressLayer.strokeColor = _progressColor.CGColor;
}
}
/**
进度条路径
@return 进度条
*/
- (UIBezierPath *)progressPathWithProgress:(CGFloat)progress
{
UIBezierPath *path = [UIBezierPath bezierPath];
CGPoint startPoint = CGPointMake(0, self.frame.size.height/2);
CGPoint endPoint = CGPointMake(self.frame.size.width*progress, self.frame.size.height/2);
[path moveToPoint:startPoint];
[path addLineToPoint:endPoint];
return path;
}
- (CAShapeLayer *)progressLayer
{
if (!_progressLayer) {
_progressLayer = [self defaultShapeLayer];
_progressLayer.strokeColor = _progressColor.CGColor;
}
return _progressLayer;
}
- (CAShapeLayer *)defaultShapeLayer
{
CAShapeLayer *layer = [CAShapeLayer layer];
layer.fillColor = [UIColor clearColor].CGColor;
layer.strokeColor = [UIColor grayColor].CGColor;
// layer.lineCap = kCALineCapRound;
// layer.lineJoin = kCALineJoinRound;
layer.frame = self.bounds;
layer.lineWidth = self.frame.size.height;
return layer;
}
- (void)setUserInteractionEnabled:(BOOL)userInteractionEnabled
{
[super setUserInteractionEnabled:userInteractionEnabled];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
@end