优化:调整送礼动画代码

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 ()
@property (nonatomic, strong) dispatch_source_t giftTimer;
@property (nonatomic, strong) dispatch_queue_t queue;
@property (nonatomic, strong) NSMutableArray<GiftReceiveInfoModel *> *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<NSString *> *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<NSString *> *)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