Vam*_*hna 7 singleton objective-c touch uigesturerecognizer ios
嗨我使用下面的代码片段来获取给定类中触摸的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);
}
Run Code Online (Sandbox Code Playgroud)
最初我只想为两个ViewControllers做这件事,但现在我想为所有类做这件事.我是否必须在每个类中编写这些片段以满足需要或者我可以使用单例实例?
这是我的Singleton类代码:
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
Run Code Online (Sandbox Code Playgroud)
在赏金后编辑:正如所建议的,我在我的框架中使用了UIViewController(比如MagicViewController),并在该控制器中编写了触摸手势跟踪片段.我把框架带到了我的客户端并要求他让他所有的类成为我的'MagicViewController'的子类,以便存储他的触摸数据,而不必编写任何其他代码(当然,他将不得不添加我们的框架)作为他的应用程序中的嵌入二进制 但他不愿意让他的类成为'MagicViewController'的子类.有没有其他方法可以达到这个目的?我见过像appanalytics.io,appsee.io这样的人跟踪触摸数据而无需编写任何代码.
通用功能属于基类。编写您自己的自定义视图控制器类MyViewController(或其他一些有用的名称),将其扩展UIViewController,并将通用视图控制器代码放入该类中。
然后让所有实际的视图控制器类扩展MyViewController而不是UIViewController. 这样,您的所有视图控制器都会将所有常见功能添加到您的基类中。
你的单例类与此无关。
| 归档时间: |
|
| 查看次数: |
178 次 |
| 最近记录: |