Disable touch events in specific areas of the iPhone screen

I want to turn off touches in all areas of the screen, except for certain several points (for example, buttons). That is, I don’t want touchhesBegan to start at all when I clicked anything but a button. Call

self.view.userInteractionEnabled = NO; 

has the desired effect in order not to register touches, but then, of course, I can’t press any buttons. I basically want the button to still work , even if 5 dots hit the screen, i.e. All touch inputs were used, and the button represents the 6th.

Is it possible?

I tried to insert a view with userInteraction disabled below my buttons, but it still registers touches when the user picks up the screen. It seems that the only way to turn off the registration of touches is to do it on the whole screen (on the parent UIView).

UPDATE: I tried using gesture recognizers to handle all touch events and ignore those that don't meet the requirements. Here is my code:

 @interface ViewController : UIViewController <UIGestureRecognizerDelegate> 

...

  - (void)viewDidLoad { [super viewDidLoad]; UIGestureRecognizer *allRecognizer = [[UIGestureRecognizer alloc] initWithTarget:self action:nil]; allRecognizer.delegate = self; [self.view addGestureRecognizer:allRecognizer]; } - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { CGPoint coords = [touch locationInView:self.view]; NSLog(@"Coords: %g, %g", coords.x, coords.y); if (coords.y < 200) { [self ignoreTouch:touch forEvent:nil]; return TRUE; } return FALSE; } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"%i touch(es)", [touches count]); } 

However, the screen still “reads” the strokes, so if I put 5 fingers down, the 6th will not press the button ...

+4
source share
7 answers

You need to configure the invisible UIButton and lay it between the view, which should not register touches, and UIButtons, which should still be active.

Now you need to set the invisible button 'userInteractionEnabled':

 //userInteractionEnabled == NO => self.view registeres touches //userInteractionEnabled == YES => self.view doesn't register touches [_invisibleButton setUserInteractionEnabled:NO]; 

What really matters in this decision is that both - the invisible and visible buttons - are direct subviews of the VC view.

You can download the sample project from my Dropbox: https://dl.dropboxusercontent.com/u/99449487/DontTapThat.zip

However, this example simply prevents the handling of some touches. Completely ignoring input is technically impossible: third-party applications are not responsible for detecting input. They are simply responsible for handling input. Touch input detection is performed by iOS.

The only way to create a case like you describe it in the comments is to hope that iOS will not interpret the input of your case as a “finger”, because it will most likely cover an area that is larger than a finger.

So, in conclusion, the best way would be to change the material of the case that you are going to build, or at least give it a non-conductive coating. From the point of view of third-party developers, there is no way to achieve your goals using software if there is a need for 5 fingers, as described in the comments.

+6
source

There are several methods in UIView that you can override:

 - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event; // recursively calls -pointInside:withEvent:. point is in the receiver coordinate system - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event; // default returns YES if point is in bounds 

This should prevent calling touchBegin and other methods.

Sincerely.

+3
source

I have a good solution for this. In which area you want to hide the interaction, hover a transparent button on top of the area.

0
source

touchhesBegan is the default method, so it should call all the time when the touch happens, so there is no way out, but you can still do one thing

 self.buttonPlayMusic.userInteractionEnabled = FALSE; 

for an object that you do not need, maybe this can help you with your desired output.

0
source

You can add a UIImageView control in this area where you want to disable the touch event. You can add a UIImageView object to the beginning of self.view subViews.

Example // area is the area where you want to disable the self.view touch button

 UIImageView *imageView=[[UIImageView alloc]initWithFrame:area]; [self.view addSubView:imageView]; 
0
source

Have you tried using UIScrollView as a background? in the area where you do not want touch events to fire.

UIScrollView does not call touch methods.

0
source

You touchDelegate will always call this way, but if you do some kind of task when you touch, you can do your task this way.

 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; UIButton *touchObject=(UIButton*)[touch view]; if ([touchObject isKindOfClass:[UIButton class]]) { //Do what ever you want on button touch } else{ return; } } 
0
source

Source: https://habr.com/ru/post/1499643/


All Articles