您好,我正在使用以下代码片段来获取给定类中触摸的X和Y坐标以及ViewController的名称:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self.view];
NSLog(@"Began: %@ X location: %0.2f", NSStringFromClass([self class]), point.x);
NSLog(@"Began: %@ Y location: %0.2f", NSStringFromClass([self class]), point.y);
}
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self.view];
NSLog(@"Ended:%@ X location: %0.2f", NSStringFromClass([self class]), point.x);
NSLog(@"Ended:%@ Y Location: %0.2f", NSStringFromClass([self class]), point.y);
}
最初我只想在两个视图控制器中执行此操作,但现在我希望在所有类中都执行。我必须在每个类中编写这些片段才能做到这一点吗?还是可以使用单例实例?
这是我的单例类代码:
QuestionAlert.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface QuestionAlert : NSObject
+(id)sharedManager;
@end
QuestionAlert.m
#import "QuestionAlert.h"
@implementation QuestionAlert
+(id)sharedManager{
static QuestionAlert *sharedMyManager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedMyManager = [[self alloc] init];
});
return sharedMyManager;
//defines a static variable sharedMyManager and is initialised only once in sharedManager
}
-(id)init{
if(self = [super init]){
//initialisations
}
return self;
}
-(void)dealloc{
//should never be called but here just for clarity
}
@end
赏金后编辑:如建议所述,我在我的框架中使用了一个UIViewController(假设为MagicViewController),并在该控制器中编写了触摸手势跟踪代码片段。我已将框架提供给客户,并要求他将所有类都作为'MagicViewController'的子类,以便在不需要编写任何其他代码的情况下存储他的触摸数据(当然,他必须添加我们的框架)作为应用程序中的嵌入式二进制文件。但是他不愿意让他的类成为'MagicViewController'的子类。有没有其他方法实现这个目的?我看到一些像appanalytics.io、appsee.io这样的人可以跟踪触摸数据而无需编写任何代码。