Files
featherVoice/QXLive/活动/QXDrawGiftCenterView.m
2025-10-20 09:43:10 +08:00

229 lines
6.9 KiB
Objective-C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// QXDrawGiftCenterView.m
// QXLive
//
// Created by 启星 on 2025/8/27.
//
#import "QXDrawGiftCenterView.h"
static CGFloat lineWidth = 13;
@implementation QXDrawGiftCenterProgressView
- (instancetype)init
{
self = [super init];
if (self) {
self.progressColor = RGB16(0x32F6CB);
self.backgroundColor = RGB16(0x70ADFA);
self.cycleLabel = [[UILabel alloc] init];
self.cycleLabel.font = [UIFont boldSystemFontOfSize:16];
self.cycleLabel.textColor = RGB16(0xffffff);
[self addSubview:self.cycleLabel];
[self.cycleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.centerY.equalTo(self);
}];
[self bringSubviewToFront:self.cycleLabel];
}
return self;
}
-(void)drawRect:(CGRect)rect{
[super drawRect:rect];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
// 计算圆心和半径
CGPoint center = CGPointMake(rect.size.width / 2, rect.size.height / 2);
CGFloat radius = MIN(ScaleWidth(70), ScaleWidth(70)) / 2 - lineWidth / 2;
// 绘制进度圆环
[self drawProgressWithCenter:center radius:radius];
CGContextRestoreGState(context);
}
- (void)setProgress:(double)progress {
_progress = MAX(0.0, MIN(1.0, progress)); // 限制在0-1之间
[self setNeedsDisplay];
self.cycleLabel.text = [NSString stringWithFormat:@"%d%%",(int)(progress*100)];
[self bringSubviewToFront:self.cycleLabel];
}
- (void)drawProgressWithCenter:(CGPoint)center radius:(CGFloat)radius {
if (self.progress > 0) {
// 计算结束角度(顺时针从顶部开始)
CGFloat endAngle = -M_PI_2 + 2 * M_PI * self.progress;
UIBezierPath *progressPath = [UIBezierPath bezierPathWithArcCenter:center
radius:radius
startAngle:-M_PI_2 // 从顶部开始(-90度
endAngle:endAngle
clockwise:YES];
[self.progressColor setStroke];
progressPath.lineWidth = lineWidth;
progressPath.lineCapStyle = kCGLineCapRound;
[progressPath stroke];
}
}
@end
@implementation QXDrawGiftCenterView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self initSubviews];
}
return self;
}
-(void)setModel:(QXGiftActivityModel *)model{
_model = model;
if (model.is_xlh.intValue == 1) {
self.hidden = NO;
}else{
self.hidden = YES;
}
double progress = (model.xlh_data.current_num.doubleValue/model.xlh_data.start_num.doubleValue);
[self setProgress:progress];
QXDrawGiftCenterType type = model.xlh_data.status.integerValue;
[self setType:type];
}
-(void)initSubviews{
self.bgImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"sky_status_bg"]];
self.bgImageView.contentMode = UIViewContentModeScaleToFill;
[self addSubview:self.bgImageView];
[self.bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.left.right.equalTo(self);
make.height.equalTo(self.bgImageView.mas_width);
}];
self.cycleBgView = [[UIView alloc] init];
self.cycleBgView.backgroundColor = RGB16(0x70ADFA);
[self.cycleBgView addRoundedCornersWithRadius:ScaleWidth(35)];
[self addSubview:self.cycleBgView];
[self.cycleBgView mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.height.mas_equalTo(ScaleWidth(70));
make.centerX.equalTo(self.bgImageView);
make.centerY.equalTo(self.bgImageView).offset(10);
}];
self.startBtn = [[UIButton alloc] init];
[self.startBtn addTarget:self action:@selector(startAction) forControlEvents:(UIControlEventTouchUpInside)];
[self addSubview:self.startBtn];
[self.startBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(ScaleWidth(92));
make.height.mas_equalTo(ScaleWidth(54));
make.centerX.equalTo(self);
make.bottom.equalTo(self);
}];
self.progressView = [[QXDrawGiftCenterProgressView alloc] init];
[self.progressView addRoundedCornersWithRadius:ScaleWidth(35)];
[self addSubview:self.progressView];
[self.progressView mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.height.mas_equalTo(ScaleWidth(70));
make.centerX.centerY.equalTo(self.cycleBgView);
// make.centerY.equalTo(self.bgImageView).offset(10);
}];
// [self startHide];
}
-(void)startAction{
switch (self.type) {
case QXDrawGiftCenterTypeWait:{
showToast(@"活动未开始");
}
break;
case QXDrawGiftCenterTypeWill:{
showToast(@"活动即将开始");
}
break;
case QXDrawGiftCenterTypeStart:{
if (self.startBlock) {
self.startBlock();
}
}
break;
default:
break;
}
}
-(void)startHide{
[UIView animateWithDuration:1 animations:^{
// self.bgImageView.alpha = 0;
self.alpha = 0;
} completion:^(BOOL finished) {
[self startShow];
}];
}
-(void)startShow{
[UIView animateWithDuration:1 animations:^{
// self.bgImageView.alpha = 1;
self.alpha = 1;
} completion:^(BOOL finished) {
[self startHide];
}];
}
- (void)setProgress:(CGFloat)progress {
_progress = progress;
self.progressView.progress = progress;
}
-(void)setActivityType:(QXActivityType)activityType{
_activityType = activityType;
switch (_activityType) {
case QXActivityTypeSky:{
self.bgImageView.image = [UIImage imageNamed:@"sky_status_bg"];
}
break;
case QXActivityTypeAge:{
self.bgImageView.image = [UIImage imageNamed:@"age_status_bg"];
}
break;
case QXActivityTypeTime:{
self.bgImageView.image = [UIImage imageNamed:@"time_status_bg"];
}
break;
default:
break;
}
}
-(void)setType:(QXDrawGiftCenterType)type{
_type = type;
switch (_type) {
case QXDrawGiftCenterTypeWait:{
[self.startBtn setBackgroundImage:[UIImage imageNamed:@"meet_status_wait"] forState:(UIControlStateNormal)];
}
break;
case QXDrawGiftCenterTypeWill:{
[self.startBtn setBackgroundImage:[UIImage imageNamed:@"meet_status_will"] forState:(UIControlStateNormal)];
}
break;
case QXDrawGiftCenterTypeStart:{
[self.startBtn setBackgroundImage:[UIImage imageNamed:@"meet_status_start"] forState:(UIControlStateNormal)];
[self setProgress:1];
}
break;
default:
break;
}
}
@end