Files
peko-ios/YuMi/Modules/YMRoom/View/AnimationView/LuckyGiftWinningBannerView.m
edwinQQQ 57735e37c4 fix: 优化送礼的内存效率 |
TODO:调整 combo 时的金币变动效果
2024-12-09 19:27:02 +08:00

296 lines
10 KiB
Objective-C

//
// LuckyGiftWinningBannerView.m
// YuMi
//
// Created by P on 2024/9/10.
//
#import "LuckyGiftWinningBannerView.h"
#import "AttachmentModel.h"
#import "i18nGiftNameMap.h"
#import "XPRoomViewController.h"
#import "XCCurrentVCStackManager.h"
// Constants
static const CGFloat kBannerWidth = 346.5f;
static const CGFloat kBannerHeight = 82.5f;
static const CGFloat kBannerTopMargin = 80.0f;
static const CGFloat kAvatarSize = 43.0f;
static const CGFloat kAnimationDuration = 0.25f;
static const CGFloat kDisplayDuration = 2.5f;
static const CGFloat kRoomTransitionDelay = 2.0f;
@interface LuckyGiftWinningBannerViewModel : PIBaseModel
@property (nonatomic, copy, readonly) NSString *times;
@property (nonatomic, copy, readonly) NSString *avatar;
@property (nonatomic, copy, readonly) NSString *coins;
@property (nonatomic, copy, readonly) NSDictionary *giftNameMap;
@property (nonatomic, copy, readonly) NSString *roomUid;
@property (nonatomic, copy, readonly) NSString *giftName;
@end
@implementation LuckyGiftWinningBannerViewModel
+ (NSDictionary *)replacedKeyFromPropertyName {
return @{@"avatar": @"sender.avatar"};
}
- (NSString *)giftName {
if (isMSRTL() && [self.giftNameMap[@"ar"] length]) {
return self.giftNameMap[@"ar"];
}
if (isMSZH() && [self.giftNameMap[@"zh"] length]) {
return self.giftNameMap[@"zh"];
}
return self.giftNameMap[@"en"] ?: @"";
}
@end
@interface LuckyGiftWinningBannerView ()
@property (nonatomic, strong) LuckyGiftWinningBannerViewModel *model;
@property (nonatomic, strong) UIImageView *backgroundImageView;
@property (nonatomic, strong) UIImageView *ballImageView;
@property (nonatomic, strong) NetImageView *avatarImageView;
@property (nonatomic, strong) UILabel *giftNameLabel;
@property (nonatomic, strong) UILabel *timesLabel;
@property (nonatomic, strong) UILabel *coinsLabel;
@property (nonatomic, assign) NSInteger currentRoomUid;
@property (nonatomic, copy) void(^completeDisplay)(void);
@property (nonatomic, copy) void(^exitCurrentRoom)(void);
@end
@implementation LuckyGiftWinningBannerView
+ (void)display:(UIView *)superView
inRoomUid:(NSInteger)roomUid
with:(AttachmentModel *)attachment
complete:(void(^)(void))complete
exitCurrentRoom:(void(^)(void))exit {
#if DEBUG
NSParameterAssert(superView);
NSParameterAssert(attachment);
#else
if (!superView || !attachment) {
if (complete) {
complete();
}
return;
}
#endif
LuckyGiftWinningBannerViewModel *model = [LuckyGiftWinningBannerViewModel modelWithDictionary:attachment.data];
CGFloat width = kGetScaleWidth(kBannerWidth);
CGFloat height = kGetScaleWidth(kBannerHeight);
CGRect frame = CGRectMake(KScreenWidth, kBannerTopMargin, width, height);
LuckyGiftWinningBannerView *bannerView = [[LuckyGiftWinningBannerView alloc] initWithFrame:frame];
bannerView.model = model;
bannerView.completeDisplay = complete;
bannerView.exitCurrentRoom = exit;
bannerView.currentRoomUid = roomUid;
[superView addSubview:bannerView];
[bannerView animateDisplay];
}
- (void)animateDisplay {
@kWeakify(self);
[UIView animateWithDuration:kAnimationDuration animations:^{
@kStrongify(self);
self.center = CGPointMake(self.superview.center.x, self.frame.size.height/2 + kBannerTopMargin);
} completion:^(BOOL finished) {
@kStrongify(self);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(kDisplayDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self animateDisappear];
});
}];
}
- (void)animateDisappear {
@kWeakify(self);
[UIView animateWithDuration:kAnimationDuration
delay:0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
@kStrongify(self);
self.frame = CGRectMake(-KScreenWidth, kBannerTopMargin, self.frame.size.width, self.frame.size.height);
} completion:^(BOOL finished) {
@kStrongify(self);
[self removeFromSuperview];
if (self.completeDisplay) {
self.completeDisplay();
}
}];
}
- (void)setModel:(LuckyGiftWinningBannerViewModel *)model {
_model = model;
self.avatarImageView.imageUrl = model.avatar;
self.giftNameLabel.text = [model giftName];
self.timesLabel.text = model.times;
self.coinsLabel.text = [NSString formatNumberToKOrM:model.coins];
}
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self setupUI];
UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
[self addSubview:b];
[b mas_remakeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(self);
}];
[b addTarget:self action:@selector(handelTap) forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
- (void)handelTap {
if (self.model.roomUid.integerValue == self.currentRoomUid) {
return;
}
@kWeakify(self);
[TTPopup alertWithMessage:YMLocalizedString(@"Combo_10") confirmHandler:^{
@kStrongify(self);
if (self.exitCurrentRoom) {
self.exitCurrentRoom();
}
NSString *targetRoomUid = self.model.roomUid;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[XPRoomViewController openRoom:targetRoomUid
viewController:[XCCurrentVCStackManager shareManager].getCurrentVC];
});
} cancelHandler:^{}];
}
- (void)setupUI {
[self addSubview:self.backgroundImageView];
[self.backgroundImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(self);
}];
[self addSubview:self.ballImageView];
[self.ballImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.trailing.bottom.top.mas_equalTo(self);
make.width.mas_equalTo(self.ballImageView.mas_height);
}];
[self addSubview:self.avatarImageView];
[self.avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.mas_equalTo(kGetScaleWidth(8));
make.top.mas_equalTo(kGetScaleWidth(24));
make.width.height.mas_equalTo(kGetScaleWidth(43));
}];
UILabel *titleLabel_1 = [UILabel labelInitWithText:YMLocalizedString(@"Combo_0") font:kFontSemibold(14) textColor:[UIColor whiteColor]];
[self addSubview:titleLabel_1];
[titleLabel_1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.avatarImageView).offset(2);
make.leading.mas_equalTo(self.avatarImageView.mas_trailing).offset(10);
}];
[self addSubview:self.giftNameLabel];
[self.giftNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.mas_equalTo(titleLabel_1);
make.leading.mas_equalTo(titleLabel_1.mas_trailing).offset(3);
}];
UILabel *titleLabel_2 = [UILabel labelInitWithText:YMLocalizedString(@"Combo_4") font:kFontSemibold(14) textColor:[UIColor whiteColor]];
[self addSubview:titleLabel_2];
[titleLabel_2 mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.mas_equalTo(self.avatarImageView.mas_bottom).offset(-2);
make.leading.mas_equalTo(self.avatarImageView.mas_trailing).offset(10);
}];
[self addSubview:self.timesLabel];
[self.timesLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.mas_equalTo(titleLabel_2);
make.leading.mas_equalTo(titleLabel_2.mas_trailing).offset(3);
}];
UILabel *titleLabel_3 = [UILabel labelInitWithText:YMLocalizedString(@"Combo_9") font:kFontSemibold(14) textColor:[UIColor whiteColor]];
[self addSubview:titleLabel_3];
[titleLabel_3 mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.mas_equalTo(self.timesLabel);
make.leading.mas_equalTo(self.timesLabel.mas_trailing).offset(3);
}];
[self addSubview:self.coinsLabel];
[self.coinsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.mas_equalTo(self.ballImageView);
make.top.mas_equalTo(self).offset(kGetScaleWidth(18));
make.height.mas_equalTo(kGetScaleWidth(28));
make.width.mas_equalTo(self.ballImageView);
}];
UILabel *titleLabel_4 = [UILabel labelInitWithText:YMLocalizedString(@"Combo_5") font:kFontSemibold(14) textColor:[UIColor whiteColor]];
[self addSubview:titleLabel_4];
[titleLabel_4 mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.mas_equalTo(self.coinsLabel);
make.top.mas_equalTo(self.coinsLabel.mas_bottom);
}];
}
#pragma mark -
- (UIImageView *)backgroundImageView {
if (!_backgroundImageView) {
_backgroundImageView = [[UIImageView alloc] initWithImage:[kImage(@"luck_gift_flag") ms_SetImageForRTL]];
_backgroundImageView.contentMode = UIViewContentModeScaleAspectFill;
}
return _backgroundImageView;
}
- (UIImageView *)ballImageView {
if (!_ballImageView) {
_ballImageView = [[UIImageView alloc] initWithImage:kImage(@"luck_gift_flag_ball")];
_ballImageView.contentMode = UIViewContentModeScaleAspectFill;
}
return _ballImageView;
}
- (NetImageView *)avatarImageView {
if (!_avatarImageView) {
NetImageConfig * config = [[NetImageConfig alloc]init];
config.placeHolder = [UIImageConstant defaultAvatarPlaceholder];
config.imageType = ImageTypeCornerAvatar;
_avatarImageView = [[NetImageView alloc] initWithConfig:config];
_avatarImageView.backgroundColor = [UIColor clearColor];
_avatarImageView.contentMode = UIViewContentModeScaleAspectFill;
[_avatarImageView setCornerRadius:kGetScaleWidth(43/2)];
}
return _avatarImageView;
}
- (UILabel *)giftNameLabel {
if (!_giftNameLabel) {
_giftNameLabel = [UILabel labelInitWithText:@"Gift" font:kFontSemibold(14) textColor:UIColorFromRGB(0xffe375)];
}
return _giftNameLabel;
}
- (UILabel *)coinsLabel {
if (!_coinsLabel) {
_coinsLabel = [UILabel labelInitWithText:@"14K" font:kFontSemibold(20) textColor:UIColorFromRGB(0xffe375)];
_coinsLabel.adjustsFontSizeToFitWidth = YES;
_coinsLabel.minimumScaleFactor = 0.5;
_coinsLabel.textAlignment = NSTextAlignmentCenter;
}
return _coinsLabel;
}
- (UILabel *)timesLabel {
if (!_timesLabel) {
_timesLabel = [UILabel labelInitWithText:@"913" font:kFontSemibold(14) textColor:UIColorFromRGB(0xffe375)];
}
return _timesLabel;
}
@end