From 471b8e945ff1b49314a6f18cfb55b22bd1b06646 Mon Sep 17 00:00:00 2001 From: edwinQQQ Date: Mon, 13 Jan 2025 15:29:58 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=EF=BC=9A=E8=B0=83=E6=95=B4?= =?UTF-8?q?=E9=80=81=E7=A4=BC=E5=8A=A8=E7=94=BB=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../View/AnimationView/GiftAnimationManager.m | 80 ++++++++++++------- 1 file changed, 53 insertions(+), 27 deletions(-) diff --git a/YuMi/Modules/YMRoom/View/AnimationView/GiftAnimationManager.m b/YuMi/Modules/YMRoom/View/AnimationView/GiftAnimationManager.m index 722120cb..9ecf06ee 100644 --- a/YuMi/Modules/YMRoom/View/AnimationView/GiftAnimationManager.m +++ b/YuMi/Modules/YMRoom/View/AnimationView/GiftAnimationManager.m @@ -14,13 +14,22 @@ @interface GiftAnimationManager () @property (nonatomic, strong) dispatch_source_t giftTimer; + +@property (nonatomic, strong) dispatch_queue_t queue; @property (nonatomic, strong) NSMutableArray *giftQueue; + @property (nonatomic, strong) GiftAnimationHelper *animationHelper; @end @implementation GiftAnimationManager +- (void)dealloc { + if (_queue) { + _queue = NULL; + } +} + - (instancetype)initWithContainerView:(UIView *)containerView { self = [super init]; if (self) { @@ -30,6 +39,7 @@ _animationInterval = 0.2; _comboAnimationDelay = 0.2; _standardAnimationDelay = 0.3; + _queue = dispatch_queue_create("com.GiftAnimationManager.queue", DISPATCH_QUEUE_SERIAL); } return self; } @@ -64,29 +74,37 @@ } - (void)processNextGift { - if (self.giftQueue.count == 0) { - [self stopGiftQueue]; - return; - } - - GiftReceiveInfoModel *giftInfo = self.giftQueue.firstObject; + dispatch_async(self.queue, ^{ + if (self.giftQueue.count == 0) { + [self stopGiftQueue]; + return; + } + + GiftReceiveInfoModel *giftInfo = self.giftQueue.firstObject; + if (!giftInfo) return; - @kWeakify(self); - dispatch_async(dispatch_get_main_queue(), ^{ - @kStrongify(self); - [self distributeGiftAnimation:giftInfo]; + @kWeakify(self); + dispatch_async(dispatch_get_main_queue(), ^{ + @kStrongify(self); + [self distributeGiftAnimation:giftInfo]; + }); + + // 安全地移除第一个元素 + if (self.giftQueue.count > 0) { + [self.giftQueue removeObjectAtIndex:0]; + } }); - - if (self.giftQueue.count > 0) { - [self.giftQueue removeObjectAtIndex:0]; - } } - (void)distributeGiftAnimation:(GiftReceiveInfoModel *)giftInfo { NSArray *targetUids = [self resolveTargetUids:giftInfo]; - CGPoint startPoint = [self calculateAnimationPoint:giftInfo.uid isEndPoint:NO]; - + CGPoint startPoint = CGPointZero; BOOL isComboAnimation = [self shouldUseComboAnimationForSender:giftInfo.uid]; + if (isComboAnimation) { + startPoint = [self comboAnimationStartPoint]; + } else { + [self calculateAnimationPoint:giftInfo.uid isEndPoint:NO]; + } NSTimeInterval delay = isComboAnimation ? self.comboAnimationDelay : self.standardAnimationDelay; for (NSString *targetUid in targetUids) { @@ -100,6 +118,10 @@ } - (NSArray *)resolveTargetUids:(GiftReceiveInfoModel *)giftInfo { + if (!giftInfo) { + return @[]; + } + if (giftInfo.isLuckyBagGift) { return @[giftInfo.targetUid]; } @@ -138,17 +160,16 @@ } - (CGPoint)calculateAnimationPoint:(NSString *)uid isEndPoint:(BOOL)isEndPoint { + if (uid.length == 0) { + return [self fallbackPointForEndPoint:isEndPoint]; + } + CGPoint point = [self.delegate animationPointAtStageViewByUid:uid]; - - if (point.x <= 0 || point.y <= 0) { - point = [self fallbackPointForEndPoint:isEndPoint]; + if (!CGPointEqualToPoint(point, CGPointZero)) { + return point; } - if ([self shouldUseComboAnimationForSender:uid] && !isEndPoint) { - point = [self comboAnimationStartPoint]; - } - - return point; + return [self fallbackPointForEndPoint:isEndPoint]; } - (void)scheduleAnimationWithDelay:(NSTimeInterval)delay @@ -168,7 +189,6 @@ - (void)stopGiftQueue { if (self.giftTimer) { - dispatch_source_cancel(self.giftTimer); // 取消定时器 dispatch_source_cancel(self.giftTimer); @@ -180,8 +200,14 @@ } - (void)enqueueGift:(GiftReceiveInfoModel *)giftInfo { - [self.giftQueue addObject:giftInfo]; - [self startGiftQueue]; + if (!giftInfo) { + return; + } + + dispatch_async(self.queue, ^{ + [self.giftQueue addObject:giftInfo]; + [self startGiftQueue]; + }); } // Helper methods