优化:调整送礼动画代码

This commit is contained in:
edwinQQQ
2025-01-13 15:29:58 +08:00
parent d71a139afb
commit 471b8e945f

View File

@@ -14,13 +14,22 @@
@interface GiftAnimationManager () @interface GiftAnimationManager ()
@property (nonatomic, strong) dispatch_source_t giftTimer; @property (nonatomic, strong) dispatch_source_t giftTimer;
@property (nonatomic, strong) dispatch_queue_t queue;
@property (nonatomic, strong) NSMutableArray<GiftReceiveInfoModel *> *giftQueue; @property (nonatomic, strong) NSMutableArray<GiftReceiveInfoModel *> *giftQueue;
@property (nonatomic, strong) GiftAnimationHelper *animationHelper; @property (nonatomic, strong) GiftAnimationHelper *animationHelper;
@end @end
@implementation GiftAnimationManager @implementation GiftAnimationManager
- (void)dealloc {
if (_queue) {
_queue = NULL;
}
}
- (instancetype)initWithContainerView:(UIView *)containerView { - (instancetype)initWithContainerView:(UIView *)containerView {
self = [super init]; self = [super init];
if (self) { if (self) {
@@ -30,6 +39,7 @@
_animationInterval = 0.2; _animationInterval = 0.2;
_comboAnimationDelay = 0.2; _comboAnimationDelay = 0.2;
_standardAnimationDelay = 0.3; _standardAnimationDelay = 0.3;
_queue = dispatch_queue_create("com.GiftAnimationManager.queue", DISPATCH_QUEUE_SERIAL);
} }
return self; return self;
} }
@@ -64,29 +74,37 @@
} }
- (void)processNextGift { - (void)processNextGift {
if (self.giftQueue.count == 0) { dispatch_async(self.queue, ^{
[self stopGiftQueue]; if (self.giftQueue.count == 0) {
return; [self stopGiftQueue];
} return;
}
GiftReceiveInfoModel *giftInfo = self.giftQueue.firstObject;
GiftReceiveInfoModel *giftInfo = self.giftQueue.firstObject;
if (!giftInfo) return;
@kWeakify(self); @kWeakify(self);
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
@kStrongify(self); @kStrongify(self);
[self distributeGiftAnimation:giftInfo]; [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 { - (void)distributeGiftAnimation:(GiftReceiveInfoModel *)giftInfo {
NSArray<NSString *> *targetUids = [self resolveTargetUids:giftInfo]; NSArray<NSString *> *targetUids = [self resolveTargetUids:giftInfo];
CGPoint startPoint = [self calculateAnimationPoint:giftInfo.uid isEndPoint:NO]; CGPoint startPoint = CGPointZero;
BOOL isComboAnimation = [self shouldUseComboAnimationForSender:giftInfo.uid]; 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; NSTimeInterval delay = isComboAnimation ? self.comboAnimationDelay : self.standardAnimationDelay;
for (NSString *targetUid in targetUids) { for (NSString *targetUid in targetUids) {
@@ -100,6 +118,10 @@
} }
- (NSArray<NSString *> *)resolveTargetUids:(GiftReceiveInfoModel *)giftInfo { - (NSArray<NSString *> *)resolveTargetUids:(GiftReceiveInfoModel *)giftInfo {
if (!giftInfo) {
return @[];
}
if (giftInfo.isLuckyBagGift) { if (giftInfo.isLuckyBagGift) {
return @[giftInfo.targetUid]; return @[giftInfo.targetUid];
} }
@@ -138,17 +160,16 @@
} }
- (CGPoint)calculateAnimationPoint:(NSString *)uid isEndPoint:(BOOL)isEndPoint { - (CGPoint)calculateAnimationPoint:(NSString *)uid isEndPoint:(BOOL)isEndPoint {
if (uid.length == 0) {
return [self fallbackPointForEndPoint:isEndPoint];
}
CGPoint point = [self.delegate animationPointAtStageViewByUid:uid]; CGPoint point = [self.delegate animationPointAtStageViewByUid:uid];
if (!CGPointEqualToPoint(point, CGPointZero)) {
if (point.x <= 0 || point.y <= 0) { return point;
point = [self fallbackPointForEndPoint:isEndPoint];
} }
if ([self shouldUseComboAnimationForSender:uid] && !isEndPoint) { return [self fallbackPointForEndPoint:isEndPoint];
point = [self comboAnimationStartPoint];
}
return point;
} }
- (void)scheduleAnimationWithDelay:(NSTimeInterval)delay - (void)scheduleAnimationWithDelay:(NSTimeInterval)delay
@@ -168,7 +189,6 @@
- (void)stopGiftQueue { - (void)stopGiftQueue {
if (self.giftTimer) { if (self.giftTimer) {
dispatch_source_cancel(self.giftTimer);
// //
dispatch_source_cancel(self.giftTimer); dispatch_source_cancel(self.giftTimer);
@@ -180,8 +200,14 @@
} }
- (void)enqueueGift:(GiftReceiveInfoModel *)giftInfo { - (void)enqueueGift:(GiftReceiveInfoModel *)giftInfo {
[self.giftQueue addObject:giftInfo]; if (!giftInfo) {
[self startGiftQueue]; return;
}
dispatch_async(self.queue, ^{
[self.giftQueue addObject:giftInfo];
[self startGiftQueue];
});
} }
// Helper methods // Helper methods