Reject the view when the user deletes any point outside the view

I have a very complex application with many multi-level views that contain many buttons, drawing areas, and other custom touch screen operations. I am showing a draggable helper view that can be on top of all other views. I need to reject this view if the user removes it beyond the scope of the subview. I tried using multiple UIWindows and adding Gesture resolvers to UIWindow.

+7
source share
3 answers

It is easy to add a transparent button whose borders are equal to the boundaries of the supervisor. And the inscription insert a transparent button below your subview.

A transparent button adds a click event that may reject the assistant view and transparent button.

For example:

UIButton *transparencyButton = [[UIButton alloc] initWithFrame:superview.bounds]; transparencyButton.backgroundColor = [UIColor clearColor]; [superview insertSubview:transparencyButton belowSubview:helperView]; [transparencyButton addTarget:self action:@selector(dismissHelper:) forControlEvents:UIControlEventTouchUpInside]; 

and the dismissHelper: method can do this:

 - (void)dismissHelper:(UIButton *)sender { [helperView dismiss]; sender.hidden = YES; // or [sender removeFromSuperview] } 
+14
source

You can check the touch of a view by going through the strokes and looking at the .view property by touch. It reflects the point of view from which the view really arises.

Assuming you keep a link to your view (either through IBOutlet or otherwise) to your view, called "myView", below works.

In your .m file. TouchhesBegan function: triggered every time the user touches a finger down. We look through the strokes to see if the original touch view is NOT equal to "myView". This comparison can also be done by checking the class, tag, or any other property that you use to identify your view. Examples below.

 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"touches began"); UITouch *touch = [touches anyObject]; if(touch.view!=myView){ myView.hidden = YES; } } 

Or if using a tag:

 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"touches began"); UITouch *touch = [touches anyObject]; if(touch.view.tag!=23){ myView.hidden = YES; } } 
+8
source

create a hittestintercept view. this can make the view below handle events as before.

 @protocol HitTestInterceptViewDelegate <NSObject> - (BOOL)interceptHitTest:(CGPoint)point withEvent:(UIEvent *)event; @end @interface HitTestInterceptView : UIView @property(nonatomic, weak) id<HitTestInterceptViewDelegate> hitTestInterceptDelegate; @end 

HtiTestInterceptView.m

import "HitTestInterceptView.h"

 @implementation HitTestInterceptView - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { if ([_hitTestInterceptDelegate interceptHitTest:point withEvent:event] == NO) { return [super hitTest:point withEvent:event]; } return nil; } @end 

then use it in toast view

 - (void)dismissIfTouchOutsideInView:(UIView *)view { if (_interceptView != nil) return; _interceptView = [[HitTestInterceptView alloc] initWithFrame:view.bounds]; _interceptView.backgroundColor = [UIColor clearColor]; _interceptView.hitTestInterceptDelegate = self; [view insertSubview:_interceptView belowSubview:self]; } - (BOOL)interceptHitTest:(CGPoint)point withEvent:(UIEvent *)event { dispatch_async(dispatch_get_main_queue(), ^{ // [self dismiss]; }); return YES; } 
+1
source

All Articles