// // QXMeetLotteryView.m // QXLive // // Created by 启星 on 2025/9/2. // #import "QXMeetLotteryView.h" #import #import "GKCycleScrollView.h" @interface QXMeetLotteryView () @property (nonatomic, strong) UIScrollView *scrollView; @property (nonatomic, assign) CGFloat itemWidth; @property (nonatomic, assign) CGFloat itemSpace; @property (nonatomic, assign) CGFloat targetOffset; @property (nonatomic, assign) CGFloat maxWidth; @property (nonatomic, strong) QXTimer *timer; @end @implementation QXMeetLotteryView - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self setup]; } return self; } - (void)setup { // 创建scrollView self.scrollView = [[UIScrollView alloc] initWithFrame:self.bounds]; self.scrollView.delegate = self; self.scrollView.showsHorizontalScrollIndicator = NO; self.scrollView.showsVerticalScrollIndicator = NO; self.scrollView.scrollEnabled = YES; // self.scrollView.hidden = YES; self.scrollView.decelerationRate = UIScrollViewDecelerationRateFast; [self addSubview:self.scrollView]; _itemSpace = 25; _itemWidth = (self.scrollView.bounds.size.width - _itemSpace*2)/3; } -(void)resetToFirstItem{ // [UIView animateWithDuration:6 animations:^{ [self.scrollView setContentOffset:CGPointZero animated:NO]; // }]; } -(void)setPrizes:(NSArray *)prizes{ _prizes = prizes; for (UIView *v in self.scrollView.subviews) { [v removeFromSuperview]; } NSInteger itemMultiple = 1; if (_prizes.count == 0) { return; } itemMultiple = 70/prizes.count; CGFloat maxWidth = 0; for (int i = 0; i < _prizes.count*itemMultiple+1; i++) { QXMeetLotterySubView *giftView = [[QXMeetLotterySubView alloc] initWithFrame:CGRectMake((_itemWidth+_itemSpace)*i, 20, _itemWidth, self.scrollView.bounds.size.height-40)]; giftView.model = self.prizes[i%self.prizes.count]; [self.scrollView addSubview:giftView]; maxWidth = (_itemWidth+_itemSpace)*i+_itemWidth; _maxWidth = maxWidth; } self.scrollView.contentSize = CGSizeMake(maxWidth, self.scrollView.bounds.size.height); } -(void)setFinalPrizeIndex:(NSInteger)finalPrizeIndex{ _finalPrizeIndex = finalPrizeIndex; _targetOffset = [self getTargetIndexOffset]; } -(void)startLotteryAnimation{ // self.scrollView.hidden = NO; [self.scrollView setContentOffset:CGPointZero animated:NO]; MJWeakSelf [UIView animateWithDuration:6 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{ // [weakSelf.scrollView setContentOffset:CGPointMake(self.targetOffset-self.scrollView.bounds.size.width, 0) animated:NO]; [weakSelf.scrollView setContentOffset:CGPointMake(self.targetOffset, 0) animated:NO]; } completion:^(BOOL finished) { // [UIView animateWithDuration:3 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{ // [weakSelf.scrollView setContentOffset:CGPointMake(self.targetOffset, 0) animated:NO]; // } completion:^(BOOL finished) { if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(lotteryWheelDidStopAtIndex:)]) { [weakSelf.delegate lotteryWheelDidStopAtIndex:weakSelf.finalPrizeIndex]; } // }]; }]; } -(void)scrollViewDidScroll:(UIScrollView *)scrollView{ } -(CGFloat)getTargetIndexOffset{ CGFloat targetOffset = 0; targetOffset = self.maxWidth-(self.itemSpace+self.itemWidth)*(3+(self.prizes.count-(self.finalPrizeIndex+1)))+self.itemSpace; return targetOffset; } - (void)stopAnimation { } @end @implementation QXMeetLotterySubView - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { [self initSubViews]; } return self; } -(void)setModel:(QXDrawGiftModel *)model{ _model = model; [self.giftImageView sd_setImageWithURL:[NSURL URLWithString:model.base_image]]; self.giftNameLabel.text = model.gift_name; [self.giftCoinBtn setTitle:[NSString stringWithFormat:@" %@",model.gift_price] forState:(UIControlStateNormal)]; } -(void)initSubViews{ self.giftBgImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ac_cycle_gift_bg"]]; [self addSubview:self.giftBgImageView]; [self.giftBgImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(self); }]; self.giftImageView = [[UIImageView alloc] init]; self.giftImageView.contentMode = UIViewContentModeScaleAspectFit; [self addSubview:self.giftImageView]; [self.giftImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(25); make.width.mas_equalTo(ScaleWidth(80)); make.height.mas_equalTo(ScaleWidth(80)); make.centerX.equalTo(self); }]; self.giftNameBgImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ac_meet_gift_name_bg"]]; self.giftNameBgImageView.contentMode = UIViewContentModeScaleToFill; [self addSubview:self.giftNameBgImageView]; [self.giftNameBgImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.equalTo(self); make.bottom.mas_equalTo(-32); make.height.mas_equalTo(30); }]; self.giftNameLabel = [[UILabel alloc] init]; self.giftNameLabel.textColor = RGB16(0xffffff); self.giftNameLabel.font = [UIFont systemFontOfSize:12]; self.giftNameLabel.textAlignment = NSTextAlignmentCenter; [self addSubview:self.giftNameLabel]; [self.giftNameLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.left.right.equalTo(self.giftNameBgImageView); make.height.mas_equalTo(15); }]; self.giftCoinBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.giftImageView.left, self.giftNameLabel.bottom, self.giftImageView.width, 15)]; [self.giftCoinBtn setTitleColor:RGB16(0xC7BF62) forState:(UIControlStateNormal)]; [self.giftCoinBtn setImage:[UIImage imageNamed:@"sky_item_coin"] forState:(UIControlStateNormal)]; self.giftCoinBtn.titleLabel.font = [UIFont systemFontOfSize:10]; [self addSubview:self.giftCoinBtn]; [self.giftCoinBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.bottom.left.right.equalTo(self.giftNameBgImageView); make.height.mas_equalTo(15); }]; } @end