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

375 lines
14 KiB
Mathematica
Raw Normal View History

2024-09-12 14:01:12 +08:00
//
// LuckyGiftWinningBannerView.m
// YuMi
//
// Created by P on 2024/9/10.
//
#import "LuckyGiftWinningBannerView.h"
2025-01-15 19:02:58 +08:00
#import <POP.h>
2024-09-12 14:01:12 +08:00
#import "AttachmentModel.h"
#import "i18nGiftNameMap.h"
#import "XPRoomViewController.h"
#import "XCCurrentVCStackManager.h"
// Constants
static const CGFloat kBannerWidth = 346.5f;
2025-04-02 11:04:07 +08:00
static const CGFloat kBannerHeight = 55.0;
2025-01-15 19:02:58 +08:00
static const CGFloat kBannerTopMargin = 0;// 80.0f;
2025-04-02 11:04:07 +08:00
//static const CGFloat kAvatarSize = 43.0f;
//static const CGFloat kAnimationDuration = 0.25f;
//static const CGFloat kDisplayDuration = 2.0f;
//static const CGFloat kRoomTransitionDelay = 2.0f;
2024-09-12 14:01:12 +08:00
@interface LuckyGiftWinningBannerViewModel : PIBaseModel
2024-09-12 14:01:12 +08:00
@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;
2024-09-12 14:01:12 +08:00
@end
@implementation LuckyGiftWinningBannerViewModel
+ (NSDictionary *)replacedKeyFromPropertyName {
return @{@"avatar": @"sender.avatar"};
2024-09-12 14:01:12 +08:00
}
- (NSString *)giftName {
if (isMSRTL() && [self.giftNameMap[@"ar"] length]) {
2024-09-12 14:01:12 +08:00
return self.giftNameMap[@"ar"];
}
if (isMSZH() && [self.giftNameMap[@"zh"] length]) {
2024-09-12 14:01:12 +08:00
return self.giftNameMap[@"zh"];
}
return self.giftNameMap[@"en"] ?: @"";
2024-09-12 14:01:12 +08:00
}
@end
@interface LuckyGiftWinningBannerView ()
@property (nonatomic, strong) LuckyGiftWinningBannerViewModel *model;
@property (nonatomic, strong) UIImageView *backgroundImageView;
@property (nonatomic, strong) UIImageView *ballImageView;
@property (nonatomic, strong) UIImageView *bigBallImageView;
@property (nonatomic, strong) NetImageView *avatarImageView;
2024-09-12 14:01:12 +08:00
@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);
2025-01-15 19:02:58 +08:00
@property (nonatomic, assign) BOOL isAnimationCancelled;
2024-09-12 14:01:12 +08:00
@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
2024-09-12 14:01:12 +08:00
LuckyGiftWinningBannerViewModel *model = [LuckyGiftWinningBannerViewModel modelWithDictionary:attachment.data];
2025-04-02 11:04:07 +08:00
CGFloat width = KScreenWidth;//kGetScaleWidth(kBannerWidth);
CGFloat height = kBannerHeight;// kGetScaleWidth(kBannerHeight);
CGRect frame = CGRectMake(KScreenWidth, kBannerTopMargin, width, height);
LuckyGiftWinningBannerView *bannerView = [[LuckyGiftWinningBannerView alloc] initWithFrame:frame];
2024-09-12 14:01:12 +08:00
bannerView.model = model;
bannerView.completeDisplay = complete;
bannerView.exitCurrentRoom = exit;
bannerView.currentRoomUid = roomUid;
[superView addSubview:bannerView];
2025-01-15 19:02:58 +08:00
[bannerView addNotification];
@kWeakify(bannerView);
[bannerView popEnterAnimation:^(BOOL finished) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
@kStrongify(bannerView);
[bannerView popLeaveAnimation:^(bool finished) {
if (bannerView.completeDisplay) {
bannerView.completeDisplay();
}
[bannerView removeFromSuperview];
}];
});
}];
}
2025-01-15 19:02:58 +08:00
- (void)addNotification {
@kWeakify(self);
2025-01-15 19:02:58 +08:00
[[NSNotificationCenter defaultCenter] addObserverForName:@"SwipeOutBanner"
object:nil
queue:[NSOperationQueue mainQueue]
usingBlock:^(NSNotification * _Nonnull notification) {
@kStrongify(self);
2025-01-15 19:02:58 +08:00
[self dismissBanner];
2024-09-12 14:01:12 +08:00
}];
}
2025-01-15 19:02:58 +08:00
- (void)dismissBanner {
[self pop_removeAllAnimations]; //
[self popLeaveAnimation:^(bool finished) {
if (self.completeDisplay) {
self.completeDisplay();
}
2025-01-15 19:02:58 +08:00
[self removeFromSuperview];
}];
}
2025-01-15 19:02:58 +08:00
- (void)popEnterAnimation:(void(^)(BOOL finished))finish {
POPSpringAnimation *enterAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPViewFrame];
2025-02-28 19:04:09 +08:00
enterAnimation.toValue = [NSValue valueWithCGRect:CGRectMake((KScreenWidth - kGetScaleWidth(kBannerWidth))/2, 0, kGetScaleWidth(kBannerWidth), kGetScaleWidth(kBannerHeight))];
2025-01-15 19:02:58 +08:00
enterAnimation.springBounciness = 10; //
enterAnimation.springSpeed = 12; //
enterAnimation.completionBlock = ^(POPAnimation *anim, BOOL finished) {
if (finish) {
finish(finished);
}
};
[self pop_addAnimation:enterAnimation forKey:@"enterAnimation"];
}
- (void)popLeaveAnimation:(void(^)(bool finished))finish {
POPBasicAnimation *exitAnimation = [POPBasicAnimation animationWithPropertyNamed:kPOPViewFrame];
exitAnimation.toValue = [NSValue valueWithCGRect:CGRectMake(-KScreenWidth, 0, kGetScaleWidth(kBannerWidth), kGetScaleWidth(kBannerHeight))];
exitAnimation.duration = 0.25; //
exitAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
exitAnimation.completionBlock = ^(POPAnimation *anim, BOOL finished) {
if (finish) {
finish(finished);
}
};
[self pop_addAnimation:exitAnimation forKey:@"exitAnimation"];
}
2024-09-12 14:01:12 +08:00
- (void)setModel:(LuckyGiftWinningBannerViewModel *)model {
_model = model;
self.avatarImageView.imageUrl = model.avatar;
2024-09-12 14:01:12 +08:00
self.giftNameLabel.text = [model giftName];
self.timesLabel.text = model.times;
self.coinsLabel.text = model.coins;
2025-04-02 11:04:07 +08:00
// if (self.coinsLabel.text.integerValue > 9999) {
// self.bigBallImageView.hidden = NO;
// } else {
// self.ballImageView.hidden = NO;
// }
2025-01-07 20:07:54 +08:00
2025-04-02 11:04:07 +08:00
// [self.coinsLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
// if (self.bigBallImageView.hidden == NO) {
// make.centerX.mas_equalTo(self.bigBallImageView);
// make.width.mas_lessThanOrEqualTo(self.bigBallImageView).multipliedBy(0.8);
// } else {
// make.centerX.mas_equalTo(self.ballImageView);
// make.width.mas_lessThanOrEqualTo(self.ballImageView).multipliedBy(0.8);
// }
// make.top.mas_equalTo(self).offset(kGetScaleWidth(18));
// make.height.mas_equalTo(kGetScaleWidth(28));
// }];
2024-09-12 14:01:12 +08:00
}
- (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;
2024-10-16 17:28:37 +08:00
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[XPRoomViewController openRoom:targetRoomUid
2024-09-12 14:01:12 +08:00
viewController:[XCCurrentVCStackManager shareManager].getCurrentVC];
});
} cancelHandler:^{}];
}
- (void)setupUI {
[self addSubview:self.backgroundImageView];
[self.backgroundImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(self);
}];
2025-04-02 11:04:07 +08:00
// [self addSubview:self.bigBallImageView];
// [self.bigBallImageView mas_makeConstraints:^(MASConstraintMaker *make) {
// make.centerY.trailing.mas_equalTo(self);
// make.width.height.mas_equalTo(kGetScaleWidth(91));
// }];
2024-09-12 14:01:12 +08:00
[self addSubview:self.ballImageView];
[self.ballImageView mas_makeConstraints:^(MASConstraintMaker *make) {
2025-04-02 11:04:07 +08:00
make.centerY.mas_equalTo(self);
make.trailing.mas_equalTo(self).offset(-30);
make.width.height.mas_equalTo(59);
2024-09-12 14:01:12 +08:00
}];
[self addSubview:self.avatarImageView];
[self.avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
2025-04-02 11:04:07 +08:00
make.centerY.mas_equalTo(self);
make.leading.mas_equalTo(kGetScaleWidth(36));
if (iPhoneXSeries) {
make.width.height.mas_equalTo(34);
} else {
make.width.height.mas_equalTo(29);
}
2024-09-12 14:01:12 +08:00
}];
UILabel *titleLabel_1 = [UILabel labelInitWithText:YMLocalizedString(@"Combo_0") font:kFontSemibold(14) textColor:[UIColor whiteColor]];
[self addSubview:titleLabel_1];
[titleLabel_1 mas_makeConstraints:^(MASConstraintMaker *make) {
2025-04-02 11:04:07 +08:00
make.top.mas_equalTo(self.avatarImageView).offset(-3);
make.leading.mas_equalTo(self.avatarImageView.mas_trailing).offset(5);
2024-09-12 14:01:12 +08:00
}];
[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);
2025-04-02 11:04:07 +08:00
make.leading.mas_equalTo(self.avatarImageView.mas_trailing).offset(5);
2024-09-12 14:01:12 +08:00
}];
[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) {
2024-09-12 18:49:42 +08:00
make.centerX.mas_equalTo(self.ballImageView);
2025-04-02 11:04:07 +08:00
make.top.mas_equalTo(self).offset(14);
make.height.mas_equalTo(22);
2025-01-07 20:07:54 +08:00
make.width.mas_lessThanOrEqualTo(self.ballImageView).multipliedBy(0.8);
2024-09-12 14:01:12 +08:00
}];
2025-04-02 11:04:07 +08:00
UILabel *titleLabel_4 = [UILabel labelInitWithText:YMLocalizedString(@"Combo_5") font:kFontMedium(12) textColor:[UIColor whiteColor]];
2024-09-12 14:01:12 +08:00
[self addSubview:titleLabel_4];
[titleLabel_4 mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.mas_equalTo(self.coinsLabel);
2025-04-02 11:04:07 +08:00
make.top.mas_equalTo(self.coinsLabel.mas_bottom).offset(-2);
2024-09-12 14:01:12 +08:00
}];
}
#pragma mark -
- (UIImageView *)backgroundImageView {
if (!_backgroundImageView) {
_backgroundImageView = [[UIImageView alloc] initWithImage:[kImage(@"luck_gift_flag") ms_SetImageForRTL]];
2025-04-02 11:04:07 +08:00
_backgroundImageView.contentMode = UIViewContentModeScaleAspectFit;
_backgroundImageView.alpha = 0.9;
2024-09-12 14:01:12 +08:00
}
return _backgroundImageView;
}
- (UIImageView *)ballImageView {
if (!_ballImageView) {
_ballImageView = [[UIImageView alloc] initWithImage:kImage(@"luck_gift_flag_ball")];
_ballImageView.contentMode = UIViewContentModeScaleAspectFill;
2025-04-02 11:04:07 +08:00
// _ballImageView.hidden = YES;
2024-09-12 14:01:12 +08:00
}
return _ballImageView;
}
- (UIImageView *)bigBallImageView {
if (!_bigBallImageView) {
_bigBallImageView = [[UIImageView alloc] initWithImage:kImage(@"luck_gift_flag_ball_BIG")];
_bigBallImageView.contentMode = UIViewContentModeScaleAspectFill;
_bigBallImageView.hidden = YES;
}
return _bigBallImageView;
}
- (NetImageView *)avatarImageView {
if (!_avatarImageView) {
2024-09-12 14:01:12 +08:00
NetImageConfig * config = [[NetImageConfig alloc]init];
config.placeHolder = [UIImageConstant defaultAvatarPlaceholder];
config.imageType = ImageTypeCornerAvatar;
_avatarImageView = [[NetImageView alloc] initWithConfig:config];
_avatarImageView.backgroundColor = [UIColor clearColor];
_avatarImageView.contentMode = UIViewContentModeScaleAspectFill;
2025-04-02 11:04:07 +08:00
[_avatarImageView setCornerRadius: iPhoneXSeries ? 34/2 : 29/2
corners:kCALayerMaxXMaxYCorner | kCALayerMaxXMinYCorner | kCALayerMinXMaxYCorner | kCALayerMinXMinYCorner
borderWidth:1
borderColor:[UIColor whiteColor]];
2024-09-12 14:01:12 +08:00
}
return _avatarImageView;
2024-09-12 14:01:12 +08:00
}
- (UILabel *)giftNameLabel {
if (!_giftNameLabel) {
_giftNameLabel = [UILabel labelInitWithText:@"" font:kFontSemibold(14) textColor:UIColorFromRGB(0xffe375)];
2024-09-12 14:01:12 +08:00
}
return _giftNameLabel;
}
- (UILabel *)coinsLabel {
if (!_coinsLabel) {
2025-04-02 11:04:07 +08:00
_coinsLabel = [UILabel labelInitWithText:@"" font:kFontSemibold(15) textColor:UIColorFromRGB(0xffe375)];
2024-09-12 14:01:12 +08:00
_coinsLabel.adjustsFontSizeToFitWidth = YES;
_coinsLabel.minimumScaleFactor = 0.5;
_coinsLabel.textAlignment = NSTextAlignmentCenter;
}
return _coinsLabel;
}
- (UILabel *)timesLabel {
if (!_timesLabel) {
_timesLabel = [UILabel labelInitWithText:@"" font:kFontSemibold(14) textColor:UIColorFromRGB(0xffe375)];
2024-09-12 14:01:12 +08:00
}
return _timesLabel;
}
@end