gpt4 book ai didi

ios - 从单例中获取触摸坐标

转载 作者:可可西里 更新时间:2023-11-01 03:30:24 26 4
gpt4 key购买 nike

您好,我正在使用以下代码片段获取给定类中触摸的 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);
}

最初我只想对两个 ViewController 执行此操作,但现在我想对所有类执行此操作。我是否必须在每个类中编写这些片段来完成必要的工作,或者我可以使用单例实例吗?

这是我的单例类代码:

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),并在该 Controller 中编写了触摸手势跟踪片段。我已经将框架带给了我的客户,并要求他将他的所有类都作为我的“MagicViewController”的子类,以便在他无需编写任何其他代码的情况下存储他的触摸数据(当然,他将不得不添加我们的框架) 作为他的应用程序中的嵌入式二进制文件。但是他不愿意让他的类成为“MagicViewController”的子类。有没有其他方法可以达到这个目的?我见过像appanalytics.io,appsee.io这样的人不需要写任何代码就可以跟踪触摸数据。

最佳答案

通用功能属于基类。编写您自己的名为 MyViewController(或其他一些有用的名称)的自定义 View Controller 类,让它扩展 UIViewController,并将您的通用 View Controller 代码放入该类中。

然后让所有实际的 View Controller 类扩展 MyViewController 而不是 UIViewController。这样,您所有的 View Controller 都会将所有常用功能添加到您的基类中。

您的单例类与此无关。

关于ios - 从单例中获取触摸坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39135628/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com