Files
featherVoice/QXLive/Room(房间)/View/礼物特效/QXGiftDisplayView.m

229 lines
7.5 KiB
Mathematica
Raw Normal View History

2025-10-20 20:05:55 +08:00
//
// QXGiftDisplayView.m
// Test
//
// Created by on 2025/10/20.
//
// QXGiftDisplayView.m
#import "QXGiftDisplayView.h"
@interface QXGiftDisplayView ()
2025-10-21 11:36:48 +08:00
@property (nonatomic, strong) UIImageView *bgImageView;
2025-10-20 20:05:55 +08:00
@property (nonatomic, strong) UIImageView *avatarImageView;
@property (nonatomic, strong) UILabel *senderLabel;
@property (nonatomic, strong) UILabel *giftLabel;
@property (nonatomic, strong) UILabel *countLabel;
@property (nonatomic, strong) UIImageView *giftImageView;
@property (nonatomic, strong) NSTimer *hideTimer;
@end
@implementation QXGiftDisplayView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self setupUI];
self.isAnimating = NO;
}
return self;
}
- (void)setupUI {
2025-10-21 11:36:48 +08:00
// self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.7];
// self.layer.cornerRadius = 20;
// self.layer.masksToBounds = YES;
self.bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 223, 40)];
self.bgImageView.image = [UIImage imageNamed:@"room_gift_display_bg"];
[self addSubview:self.bgImageView];
2025-10-20 20:05:55 +08:00
//
2025-10-21 11:36:48 +08:00
self.avatarImageView = [[UIImageView alloc] initWithFrame:CGRectMake(4, 4, 32, 32)];
2025-10-20 20:05:55 +08:00
self.avatarImageView.layer.cornerRadius = 15;
self.avatarImageView.layer.masksToBounds = YES;
self.avatarImageView.backgroundColor = [UIColor lightGrayColor];
self.avatarImageView.contentMode = UIViewContentModeScaleAspectFill;
[self addSubview:self.avatarImageView];
//
2025-10-21 11:36:48 +08:00
self.senderLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.avatarImageView.right+4, 4, 120, 16)];
self.senderLabel.font = [UIFont boldSystemFontOfSize:14];
2025-10-20 20:05:55 +08:00
self.senderLabel.textColor = [UIColor whiteColor];
[self addSubview:self.senderLabel];
//
2025-10-21 11:36:48 +08:00
self.giftLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.senderLabel.left, self.bgImageView.height-4-14, 120, 14)];
self.giftLabel.font = [UIFont systemFontOfSize:12];
self.giftLabel.textColor = RGB16(0xffffff);
2025-10-20 20:05:55 +08:00
[self addSubview:self.giftLabel];
//
2025-10-21 11:36:48 +08:00
self.giftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.bgImageView.width-self.bgImageView.height, 0, self.bgImageView.height, self.bgImageView.height)];
2025-10-20 20:05:55 +08:00
self.giftImageView.contentMode = UIViewContentModeScaleAspectFit;
[self addSubview:self.giftImageView];
//
2025-10-21 11:36:48 +08:00
self.countLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.bgImageView.right+8, 5, 100, 35)];
self.countLabel.font = [UIFont fontWithName:@"DIN Condensed" size:35];
self.countLabel.textColor = RGB16(0xFFF49F);
self.countLabel.textAlignment = NSTextAlignmentLeft;
2025-10-20 20:05:55 +08:00
[self addSubview:self.countLabel];
//
self.frame = CGRectMake(-self.bounds.size.width, self.frame.origin.y, self.bounds.size.width, self.bounds.size.height);
}
2025-10-21 11:36:48 +08:00
-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
UIView *hitView= [super hitTest:point withEvent:event];
if (hitView== self)
{
return nil;
}
else
{
return hitView;
}
}
2025-10-20 20:05:55 +08:00
// updateUIWithGift
- (void)updateUIWithGift:(QXRoomChatListModel *)gift {
if (!gift) return;
//
2025-10-21 14:28:18 +08:00
self.senderLabel.text = [NSString stringWithFormat:@"%@",gift.FromUserInfo.nickname?: @""];
2025-10-20 20:05:55 +08:00
//
2025-10-21 14:28:18 +08:00
self.giftLabel.text = [NSString stringWithFormat:@"%@送给%@",gift.ToUserInfo.nickname?: @"", gift.GiftInfo.gift_name ?: @"礼物"];
2025-10-20 20:05:55 +08:00
//
self.countLabel.text = [NSString stringWithFormat:@"x%@", gift.gift_num];
// 使SDWebImage
[self.avatarImageView sd_setImageWithURL:[NSURL URLWithString:gift.FromUserInfo.avatar]];
//
[self.giftImageView sd_setImageWithURL:[NSURL URLWithString:gift.GiftInfo.base_image]];
}
//
- (UIColor *)randomColor {
CGFloat red = arc4random_uniform(255) / 255.0;
CGFloat green = arc4random_uniform(255) / 255.0;
CGFloat blue = arc4random_uniform(255) / 255.0;
return [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
}
- (void)showGift:(QXRoomChatListModel *)gift {
//
if (self.isAnimating) {
NSLog(@"警告: 视图正在动画中,无法显示新礼物");
return;
}
self.currentGift = gift;
self.isAnimating = YES;
// UI -
[self updateUIWithGift:gift];
//
self.frame = CGRectMake(-self.bounds.size.width, self.frame.origin.y, self.bounds.size.width, self.bounds.size.height);
self.alpha = 1.0;
//
[UIView animateWithDuration:0.5
delay:0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
self.frame = CGRectMake(10, self.frame.origin.y, self.bounds.size.width, self.bounds.size.height);
} completion:^(BOOL finished) {
if (finished) {
[self startHideTimer];
} else {
//
self.isAnimating = NO;
self.currentGift = nil;
}
}];
}
- (void)updateGiftCount:(NSInteger)count {
if (!self.isAnimating) {
NSLog(@"警告: 视图不在动画中,无法更新数量");
return;
}
NSLog(@"更新礼物数量: %ld", (long)count);
//
self.countLabel.text = [NSString stringWithFormat:@"x%ld", (long)count];
//
[UIView animateWithDuration:0.2 animations:^{
self.countLabel.transform = CGAffineTransformMakeScale(1.5, 1.5);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.2 animations:^{
self.countLabel.transform = CGAffineTransformIdentity;
}];
//
[self resetHideTimer];
}];
}
- (void)startHideTimer {
[self.hideTimer invalidate];
self.hideTimer = [NSTimer scheduledTimerWithTimeInterval:3.0
target:self
selector:@selector(hideAnimation)
userInfo:nil
repeats:NO];
}
- (void)resetHideTimer {
[self.hideTimer invalidate];
[self startHideTimer];
}
- (void)hideAnimation {
if (!self.isAnimating) {
return;
}
[UIView animateWithDuration:0.5
animations:^{
self.frame = CGRectMake(-self.bounds.size.width, self.frame.origin.y, self.bounds.size.width, self.bounds.size.height);
self.alpha = 0.0;
} completion:^(BOOL finished) {
self.isAnimating = NO;
if ([self.delegate respondsToSelector:@selector(QXGiftDisplayViewDidFinishAnimation:)]) {
[self.delegate QXGiftDisplayViewDidFinishAnimation:self];
}
self.currentGift = nil;
}];
}
- (void)finishAnimationImmediately {
NSLog(@"立即结束动画");
[self.hideTimer invalidate];
self.hideTimer = nil;
[self.layer removeAllAnimations];
self.isAnimating = NO;
self.currentGift = nil;
self.alpha = 0.0;
self.frame = CGRectMake(-self.bounds.size.width, self.frame.origin.y, self.bounds.size.width, self.bounds.size.height);
}
- (void)dealloc {
[self.hideTimer invalidate];
self.hideTimer = nil;
NSLog(@"QXGiftDisplayView dealloc");
}
@end