在屏幕上的任何位置检测触摸 [英] Detecting a touch anywhere on the screen

查看:198
本文介绍了在屏幕上的任何位置检测触摸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道用户何时触摸了我的应用程序屏幕上的任何位置。



我已经使用 - (UIResponder *)nextResponder 查看,但很遗憾,这不会工作,因为我也自动重新加载表,所以这发生时会触发。



我也尝试过一个手势识别器,用下面的代码。但这只会识别触摸视图。在那里我有很多按钮,用户将使用来操作应用程序。我想避免在屏幕上的每个按钮和段控件中添加手势识别器或代码。

  UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOnView :)]; 
[self.mainView addGestureRecognizer:tap];

- (void)tapOnView:(UITapGestureRecognizer *)sender
{
// do something
}
pre>

我也尝试过 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 同样的问题作为手势识别器。



我想知道是否有任何方式,我可以实现这个任务。我希望我可以从nextResponder中识别事件的类型,然后我可以检测它是否是按钮例如。



编辑:我工作的原因是,我的应用程序需要保持活动,屏幕不能锁定(所以我已禁用屏幕锁定)。为了避免过度使用电源,我需要调暗屏幕,但一旦应用程序被触摸后,然后将亮度恢复到原来的水平。

解决方案

您可以附加 UITapGestureRecognizer



或者,您可以覆盖

有你的根的 hitTest: UIView 你希望完成的特定任务?



编辑:使用 hitTest:

  @interface PassthroughView:UIView 
@property(readonly)
@property(readonly)SEL selector;
@end
@implementation PassthroughView
- (void)setTarget:(id)target selector:(SEL)selector {
_target = target;
_selector = selector;
}
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
[_target performSelector:_selector];
return nil;
}
@end


@implementation YourUIViewController {
PassthroughView * anytouchView;
}
- (void)viewDidLoad {
//添加到结尾,因此它高于所有其他视图。
anytouchView = [[PassthroughView alloc] initWithFrame:self.view.bounds];
[anytouchView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
[anytouchView setTarget:self selector:@selector(undim)];
[anytouchView setHidden:YES];
[self.view addSubview:anytouchView];
}
- (void)undim {
[anytouchView setHidden:YES];
}
- (void)dim {
[anytouchView setHidden:NO];
}
@end


I am wanting to know when a user has touched anywhere on the screen of my app.

I have looked into using -(UIResponder *)nextResponder but unfortunately this will not work, as I am also reloaded a table automatically, so this gets trigged when that occurs.

I have also tried a gesture recognizer, with the following code. But this will only recognise touches on the view. Where as I have many buttons the user will be using to operate the app. I would like to avoid adding a gesture recogniser or code for this in every button and segment control I have on the screen

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOnView:)];
[self.mainView addGestureRecognizer:tap];

- (void)tapOnView:(UITapGestureRecognizer *)sender
{
    //do something
}

I have also tried -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event , but this has the same issue as the gesture recognizer.

I was wondering if there is any way I could achieve this task. I was hoping that I may be able to recognise the type of event from within the nextResponder, and then I could detect if it is button for example.

EDIT: The reason I am working on this is that my app needs to stay active and the screen cannot be locked (so I have disabled screen locking). To avoid excessive use of power, I need to dim the screen, but then return the brightness back to the original level once the app is touched. I need this feature to only occur on 1 of my viewcontrollers.

解决方案

You could attach your UITapGestureRecognizer to your [[UIApplication sharedApplication] keyWindow].

Alternatively, you could override hitTest: of your root UIView.

Is there a particular task you are hoping to accomplish? There may be a better way than assigning an "anywhere" gesture.

Edit: Use hitTest:.

@interface PassthroughView : UIView
@property (readonly) id target;
@property (readonly) SEL selector;
@end
@implementation PassthroughView
- (void)setTarget:(id)target selector:(SEL)selector {
  _target = target;
  _selector = selector;
}
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
  [_target performSelector:_selector];
  return nil;
}
@end


@implementation YourUIViewController {
  PassthroughView *anytouchView;
}
- (void)viewDidLoad {
  // Add this at the end so it's above all other views.
  anytouchView = [[PassthroughView alloc] initWithFrame:self.view.bounds];
  [anytouchView setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];
  [anytouchView setTarget:self selector:@selector(undim)];
  [anytouchView setHidden:YES];
  [self.view addSubview:anytouchView];
}
- (void)undim {
  [anytouchView setHidden:YES];
}
- (void)dim {
  [anytouchView setHidden:NO];
}
@end

这篇关于在屏幕上的任何位置检测触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆