从单身人士获取触摸坐标

时间:2016-08-25 02:06:49

标签: ios objective-c singleton touch uigesturerecognizer

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

最初我只想为两个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

在赏金之后编辑:正如建议的那样,我在我的框架中使用了UIViewController(比如MagicViewController),并在该控制器中编写了触摸手势跟踪片段。我把框架带到了我的客户端,并要求他把他所有的类都作为我的MagicViewController&#39;的子类,以便存储他的触摸数据,而无需编写任何其他代码(当然,他会必须在我的app中添加我们的框架)作为嵌入式二进制文件。但他不愿意让他的课成为MagicViewController&#39;的子类。有没有其他方法可以达到这个目的?我见过像appanalytics.io,appsee.io这样的人跟踪触摸数据而无需编写任何代码。

3 个答案:

答案 0 :(得分:3)

常用功能属于基类。编写自己的名为MyViewController的自定义视图控制器类(或其他一些有用的名称),让它扩展UIViewController,并将您的公共视图控制器代码放入该类中。

然后让所有实际的视图控制器类扩展MyViewController而不是UIViewController。这样,所有视图控制器都将具有添加到基类的所有常用功能。

你的单身人士课与此无关。

答案 1 :(得分:3)

只需创建UIViewController的扩展名,然后让您的客户(在编辑后回答)导入您导入此扩展程序的库标题。完成后,每个View Controller都将使用以下方法:

extension UIViewController {
 override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent)
 {
   //The Swift implementation you need to execute in all UIViewController instances
 }

 override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent)
 {
   //The Swift implementation you need to execute in all UIViewController instances
 }
}

编辑:上面是Swift。对于Obj-C,你可以创建一个UIViewController类,如下所示:

@interface UIViewController (TouchesAdditions)
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event;
@end

@implementation UIViewController (TouchesAdditions)
- (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);
}
@end

答案 2 :(得分:1)

这可以通过方法调配来实现。不需要子类。

方法调配是更改现有选择器的实现的过程。这是一种技术,通过改变选择器如何映射到类的调度表中的底层函数,可以在运行时更改Objective-C中的方法调用。 校验: http://nshipster.com/method-swizzling/