Touch Detection anywhere on the screen

I want to know when a user touched the screen of my application.

I have considered using - (UIResponder *) nextResponder , but unfortunately this will not work, as I also automatically reload the table, so it fires when this happens.

I also tried a gesture recognizer with the following code. But this only recognizes the touch of opinion. When I have many buttons, the user will use to control the application. I would like to avoid adding a gesture recognizer or code for this in each button and managing the segments that 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 also tried - (void) touchhesBegan: (NSSet *) concerns Event: (UIEvent *) event , but it has the same problem as gesture recognizer.

I was wondering if I can achieve this. I was hoping I could recognize the type of event from the next Responder, and then I could determine if this is a button, for example.

EDIT: The reason I am working on this is because my application must remain active and the screen cannot be locked (so I locked the screen lock). To avoid excessive use of power, I need to smooth the screen, but then return the brightness back to its original level as soon as the application touches. I need this function only for one of my view controllers.

+4
4

Ian MacDonald, hitTest:: - , , ..

UIWindow hitTest.

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {

   // do your stuff here

   // return nil if you want to prevent interaction with UI elements
   return [super hitTest:point withEvent:event];
}
+9

UITapGestureRecognizer [[UIApplication sharedApplication] keyWindow].

hitTest: UIView.

, ? , " ".

: 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 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
+3

.

, , , ( ). , , , .

, , , , Tap. .

, , , . , , .

+2

, .

, UIWindow .

.

, .

#import <QuartzCore/QuartzCore.h>

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.view setMultipleTouchEnabled:YES];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    // Enumerate over all the touches 
    [touches enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {
        // Get a single touch and it location
        UITouch *touch = obj;
        CGPoint touchPoint = [touch locationInView:self.view];
        ...
    }];
}

To disable the screen lock , I used the code below:

[[UIApplication sharedApplication] setIdleTimerDisabled:YES];

I used the following functions to decrease or increase the brightness of the screen

[[UIScreen mainScreen] setBrightness:0.0f]; //and
[[UIScreen mainScreen] setBrightness:1.0f];
+1
source

All Articles