Files
peko-ios/YuMi/Modules/YMRoom/View/AnimationView/LuckyGiftWinningFlagView.m

213 lines
7.5 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.

//
// LuckyGiftWinningFlagView.m
// YuMi
//
// Created by P on 2024/9/10.
//
#import "LuckyGiftWinningFlagView.h"
#import "AttachmentModel.h"
@interface LuckyGiftWinningFlagViewModel : PIBaseModel
@property (nonatomic, assign) NSInteger uid;
@property (nonatomic, assign) NSInteger level;
@property (nonatomic, assign) NSInteger roomId;
@property (nonatomic, copy) NSString *times;
@property (nonatomic, copy) NSString *coins;
@property (nonatomic, copy) NSString *roomUid;
@end
@implementation LuckyGiftWinningFlagViewModel
@end
@interface LuckyGiftWinningFlagView ()
@property (nonatomic, strong) LuckyGiftWinningFlagViewModel *model;
@property (nonatomic, strong) UILabel *winLabel;
@property (nonatomic, strong) UILabel *winPriceLabel;
@property (nonatomic, strong) UILabel *winTimesLabel;
@property (nonatomic, strong) UIImageView *coinIcon;
@property (nonatomic, strong) UIImageView *backgroundImageView;
@end
@implementation LuckyGiftWinningFlagView
+ (void)display:(UIView *)superView
with:(AttachmentModel *)attachment
roomID:(NSInteger)roomID
uID:(NSString *)UID
{
LuckyGiftWinningFlagViewModel *model = [LuckyGiftWinningFlagViewModel modelWithDictionary:attachment.data];
if (model.roomId != roomID || model.uid != UID.integerValue) {
return;
}
LuckyGiftWinningFlagView *winningFlagView = [[LuckyGiftWinningFlagView alloc] init];
winningFlagView.model = model;
winningFlagView.alpha = 0;
winningFlagView.frame = CGRectMake(0, 0, kGetScaleWidth(162), kGetScaleWidth(162));
// 🔧 修复:重新计算位置,确保视图在屏幕可见范围内
CGFloat viewWidth = kGetScaleWidth(162);
CGFloat viewHeight = kGetScaleWidth(162);
CGFloat screenWidth = KScreenWidth;
CGFloat screenHeight = KScreenHeight;
// 计算安全的 Y 位置:屏幕高度的 35% 位置
CGFloat safeY = screenHeight * 0.35;
// 确保视图不会超出屏幕边界
CGFloat maxY = screenHeight - viewHeight/2 - 20; // 留20点边距
CGFloat minY = viewHeight/2 + 20; // 留20点边距
CGFloat finalY = MAX(minY, MIN(safeY, maxY));
winningFlagView.center = CGPointMake(screenWidth/2, finalY);
winningFlagView.transform = CGAffineTransformMakeScale(0.1, 0.1);
NSLog(@"🎯 LuckyGiftWinningFlagView: 位置计算 - screenSize: %.0fx%.0f, viewSize: %.0fx%.0f, center: (%.0f, %.0f)",
screenWidth, screenHeight, viewWidth, viewHeight, winningFlagView.center.x, winningFlagView.center.y);
[superView addSubview:winningFlagView];
// 使用弹簧动画执行放大动画alpha从0变为1带有弹性效果
[UIView animateWithDuration:0.8
delay:0
usingSpringWithDamping:0.3 // 弹性系数,数值越小弹性越强
initialSpringVelocity:0.5 // 初始速度
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
winningFlagView.alpha = 1.0;
winningFlagView.transform = CGAffineTransformMakeScale(1.0, 1.0); // 还原为正常大小
} completion:^(BOOL finished) {
// 动画完成后等待2秒再执行反向动画
[UIView animateWithDuration:0.5
delay:2.0 // 延迟2秒执行反向动画
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
winningFlagView.alpha = 0.0;
winningFlagView.transform = CGAffineTransformMakeScale(0.1, 0.1); // 缩小回原始大小
} completion:^(BOOL finished) {
// 动画完成后移除 view
[winningFlagView removeFromSuperview];
}];
}];
}
- (instancetype)init
{
self = [super init];
if (self) {
[self setupUI];
}
return self;
}
- (void)setupUI {
[self addSubview:self.backgroundImageView];
[self.backgroundImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(self);
}];
[self addSubview:self.winLabel];
[self.winLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.mas_equalTo(self);
make.top.mas_equalTo(kGetScaleWidth(40));
}];
UIStackView *stackView = [[UIStackView alloc] init];
stackView.spacing = 6;
[self addSubview:stackView];
[stackView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.mas_equalTo(self.backgroundImageView);
make.width.mas_lessThanOrEqualTo(self.backgroundImageView);
// make.leading.trailing.mas_equalTo(self.backgroundImageView).inset(4);
make.top.mas_equalTo(self.winLabel.mas_bottom).offset(11);
make.height.mas_equalTo(35);
}];
[stackView addArrangedSubview:self.coinIcon];
[stackView addArrangedSubview:self.winPriceLabel];
UIStackView *stackView_2 = [[UIStackView alloc] init];
stackView_2.spacing = 4;
[self addSubview:stackView_2];
[stackView_2 mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.mas_equalTo(self.backgroundImageView);
make.width.mas_lessThanOrEqualTo(self.backgroundImageView);
// make.leading.trailing.mas_equalTo(self.backgroundImageView).inset(4);
make.top.mas_equalTo(stackView.mas_bottom).offset(11);
make.height.mas_equalTo(17);
}];
UILabel *titleLeft = [UILabel labelInitWithText:YMLocalizedString(@"Combo_4") font:kFontRegular(12) textColor:[UIColor whiteColor]];
UILabel *titleRight = [UILabel labelInitWithText:YMLocalizedString(@"Combo_9") font:kFontRegular(12) textColor:[UIColor whiteColor]];
[stackView_2 addArrangedSubview:titleLeft];
[stackView_2 addArrangedSubview:self.winTimesLabel];
[stackView_2 addArrangedSubview:titleRight];
}
- (void)setModel:(LuckyGiftWinningFlagViewModel *)model {
_model = model;
self.winPriceLabel.text = model.coins;
self.winTimesLabel.text = model.times;
self.backgroundImageView.image = model.level == 1 ? kImage(@"luck_gift_gold") : kImage(@"luck_gift_pruple");
[[NSNotificationCenter defaultCenter] postNotificationName:@"receiveLuckGiftWinning" object:model.coins];
}
#pragma mark -
- (UIImageView *)coinIcon {
if (!_coinIcon) {
_coinIcon = [[UIImageView alloc] initWithImage:kImage(@"moli_money_icon")];
_coinIcon.contentMode = UIViewContentModeScaleAspectFit;
}
return _coinIcon;
}
- (UIImageView *)backgroundImageView {
if (!_backgroundImageView) {
_backgroundImageView = [[UIImageView alloc] initWithImage:kImage(@"luck_gift_gold")];
_backgroundImageView.contentMode = UIViewContentModeScaleAspectFit;
}
return _backgroundImageView;
}
- (UILabel *)winLabel {
if (!_winLabel) {
_winLabel = [UILabel labelInitWithText:YMLocalizedString(@"Combo_4") font:kFontSemibold(14) textColor:UIColorFromRGB(0xffe375)];
_winLabel.textAlignment = NSTextAlignmentCenter;
}
return _winLabel;
}
- (UILabel *)winPriceLabel {
if (!_winPriceLabel) {
_winPriceLabel = [UILabel labelInitWithText:@"" font:kFontSemibold(26) textColor:UIColorFromRGB(0xffe375)];
_winPriceLabel.adjustsFontSizeToFitWidth = YES;
_winPriceLabel.minimumScaleFactor = 0.5f;
}
return _winPriceLabel;
}
- (UILabel *)winTimesLabel {
if (!_winTimesLabel) {
_winTimesLabel = [UILabel labelInitWithText:@"" font:kFontSemibold(14) textColor:UIColorFromRGB(0xffe375)];
_winTimesLabel.adjustsFontSizeToFitWidth = YES;
_winTimesLabel.minimumScaleFactor = 0.5f;
}
return _winTimesLabel;
}
@end