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

164 lines
5.2 KiB
Objective-C

//
// GestureOptimizer.m
// YuMi
//
// Created by AI Assistant on 2025/1/13.
//
#import "GestureOptimizer.h"
@interface GestureOptimizer ()
@property (nonatomic, strong) NSMutableDictionary *boundaryCache;
@end
@implementation GestureOptimizer
#pragma mark - Singleton
+ (instancetype)sharedOptimizer {
static GestureOptimizer *instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[GestureOptimizer alloc] init];
});
return instance;
}
#pragma mark - Initialization
- (instancetype)init {
if (self = [super init]) {
_boundaryCache = [NSMutableDictionary dictionary];
NSLog(@"🎯 GestureOptimizer: 初始化完成");
}
return self;
}
#pragma mark - Public Methods
- (GestureAreaType)gestureAreaTypeForPoint:(CGPoint)point
containerWidth:(CGFloat)containerWidth {
// 计算区域边界
CGFloat leftBoundary = containerWidth / 6.0; // 1/6 宽度
CGFloat rightBoundary = containerWidth * 5.0 / 6.0; // 5/6 宽度
if (point.x < leftBoundary) {
return GestureAreaType_Left;
} else if (point.x > rightBoundary) {
return GestureAreaType_Right;
} else {
return GestureAreaType_Center;
}
}
- (BOOL)shouldGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
receiveTouchAtPoint:(CGPoint)point
containerWidth:(CGFloat)containerWidth {
GestureAreaType areaType = [self gestureAreaTypeForPoint:point
containerWidth:containerWidth];
if ([gestureRecognizer isKindOfClass:[UISwipeGestureRecognizer class]]) {
// Swipe 手势只在中央区域生效
BOOL shouldReceive = (areaType == GestureAreaType_Center);
if (shouldReceive) {
NSLog(@"🎯 GestureOptimizer: Swipe 手势在中央区域生效 - 点: %@", NSStringFromCGPoint(point));
}
return shouldReceive;
} else if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) {
// Tap 手势根据容器类型决定区域
BOOL shouldReceive = NO;
// 根据手势识别器所在容器的 tag 来判断区域
if (gestureRecognizer.view.tag == 1001) { // 中央容器 tag 为 1001
// 中央区域的 tap 手势
shouldReceive = (areaType == GestureAreaType_Center);
} else if (gestureRecognizer.view.tag == 1002) { // 左侧容器 tag 为 1002
// 左侧区域的 tap 手势
shouldReceive = (areaType == GestureAreaType_Left);
} else if (gestureRecognizer.view.tag == 1003) { // 右侧容器 tag 为 1003
// 右侧区域的 tap 手势
shouldReceive = (areaType == GestureAreaType_Right);
}
if (shouldReceive) {
NSLog(@"🎯 GestureOptimizer: Tap 手势在 %@ 区域生效 (容器tag: %ld) - 点: %@",
[self areaTypeString:areaType], (long)gestureRecognizer.view.tag, NSStringFromCGPoint(point));
}
return shouldReceive;
}
return YES;
}
- (NSDictionary *)getAreaBoundariesForContainerWidth:(CGFloat)containerWidth {
NSString *widthKey = [NSString stringWithFormat:@"%.0f", containerWidth];
// 检查缓存
if (self.boundaryCache[widthKey]) {
return self.boundaryCache[widthKey];
}
// 计算边界
CGFloat leftBoundary = containerWidth / 6.0;
CGFloat rightBoundary = containerWidth * 5.0 / 6.0;
NSDictionary *boundaries = @{
@"leftBoundary": @(leftBoundary),
@"rightBoundary": @(rightBoundary),
@"containerWidth": @(containerWidth)
};
// 缓存结果
self.boundaryCache[widthKey] = boundaries;
NSLog(@"🎯 GestureOptimizer: 计算区域边界 - 宽度: %.0f, 左边界: %.1f, 右边界: %.1f",
containerWidth, leftBoundary, rightBoundary);
return boundaries;
}
- (void)debugGestureAreaForPoint:(CGPoint)point
containerWidth:(CGFloat)containerWidth {
GestureAreaType areaType = [self gestureAreaTypeForPoint:point
containerWidth:containerWidth];
NSDictionary *boundaries = [self getAreaBoundariesForContainerWidth:containerWidth];
NSLog(@"🎯 GestureOptimizer 调试信息:");
NSLog(@" - 触摸点: %@", NSStringFromCGPoint(point));
NSLog(@" - 容器宽度: %.0f", containerWidth);
NSLog(@" - 左边界: %.1f", [boundaries[@"leftBoundary"] floatValue]);
NSLog(@" - 右边界: %.1f", [boundaries[@"rightBoundary"] floatValue]);
NSLog(@" - 区域类型: %@", [self areaTypeString:areaType]);
}
#pragma mark - Private Methods
- (NSString *)areaTypeString:(GestureAreaType)areaType {
switch (areaType) {
case GestureAreaType_Left:
return @"左侧区域";
case GestureAreaType_Center:
return @"中央区域";
case GestureAreaType_Right:
return @"右侧区域";
default:
return @"未知区域";
}
}
- (void)clearCache {
[_boundaryCache removeAllObjects];
NSLog(@"🎯 GestureOptimizer: 清空边界缓存");
}
@end