Get touches when userinteractionenabled is NO - ios

There seems to be an answer, but when I try the method provided, it just doesn't work!
#interface MyView : UIView
#end
#implementation MyView
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
UIView *hitView = [super hitTest:point withEvent:event];
NSLog(#"hitView:%#", hitView);
if (hitView == self) {
return nil;
}
return hitView;
}
- (void)testUserInteraction
{
UIViewController *vc = [[UIViewController alloc] init];
self.window.rootViewController = vc;
MyView *myView = [[MyView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];
myView.userInteractionEnabled = NO;
[vc.view addSubview:myView];
UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 50, 50)];
subView.backgroundColor = [UIColor orangeColor];
[myView addSubview:subView];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapped)];
[subView addGestureRecognizer:tap];
}
When myView.userInteractionEnabled = YES; everything works fine. but when myView.userInteractionEnabled = NO; wherever I tapped the screen, it just output hitView:(null)
So is this method no longer working, or did I miss something?

yes, because you disabled user interaction.
When you call super-hittest,the return from super-hittest depends on userInteraction property.
If it is enabled then only, it returns either itself or some subView.
If not enabled, it will always return nil.

If I understand correctly, you want to know when a touch happens inside your custom UIView, while user interaction on the view is disabled. If this is so, throw the following inside the - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event method. This condition should read only touch events inside your custom view
if ([self pointInside:point withEvent:event]) {
NSLog(#"TOUCH IS INSIDE THE VIEW");
}

Related

iOS animation presentationLayer hitTest does not seem to work as expected

I'm trying to add touch events to small view blocks drifting from screen right side to left side. The problem is layer.presentationLayer hitTest:point method returns nothing, even if I do actually tap within the bounds of the view block.
Finally, I find a workaround. But, two questions still confuse me.
The converted point does not seem to be right, how to properly use presentationLayer hitTest?
In my code snippet, UIViewAnimationOptionAllowUserInteraction is not set, why I can handle touch event anyway?
The following are the code, any help will be appreciated
viewDidLoad
CGRect bounds = [[UIScreen mainScreen] bounds];
for (int i = 0; i < 15; i++) {
ViewBlock *view = [[ViewBlock alloc] initWithFrame:CGRectMake(bounds.size.width, 200, 40, 40)];
[self.view addSubview:view];
[UIView animateWithDuration:20 delay:i * 21 options:UIViewAnimationOptionCurveLinear animations:^{
view.frame = CGRectMake(-40, 200, 40, 40);
} completion:^(BOOL finished) {
[view removeFromSuperview];
}];
}
ViewBlock.m
#implementation ViewBlock
- (instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
self.backgroundColor = [UIColor redColor];
}
return self;
}
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
CALayer *presentationLayer = self.layer.presentationLayer; //Do NOT modify presentationLayer
if (presentationLayer) {
CGPoint layer_point = [presentationLayer convertPoint:point fromLayer:self.layer.modelLayer];
if ([presentationLayer hitTest:point]) {
return self; //not worked
}
if ([presentationLayer hitTest:layer_point]) {
return self; //not worked either
}
if (CGRectContainsPoint(presentationLayer.bounds, layer_point)) {
return self; //the workaround
}
}
return [super hitTest:point withEvent:event];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[super touchesBegan:touches withEvent:event];
NSLog(#"touches began");
}
- (void)didMoveToSuperview{
[super didMoveToSuperview];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapTouched:)]
;
tap.cancelsTouchesInView = NO;
[self addGestureRecognizer:tap];
}
- (void)tapTouched:(UITapGestureRecognizer *)sender{
NSLog(#"touches gesture");
}
#end
Q1: The converted point does not seem to be right, how to properly use presentationLayer hitTest?
What you are missing is the parameter of hitTest: should be in the coordinate system of the receiver's super layer, so the code should be:
CGPoint superLayerP = [presentationLayer.superlayer convertPoint:layer_point fromLayer:presentationLayer];
if ([presentationLayer hitTest:superLayerP]) {
return self;
}
Q2:In my code snippet, UIViewAnimationOptionAllowUserInteraction is not set, why I can handle touch event anyway
I think you can get touch event because you override - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event and return self as result, so the event will pass to it. By default(If you don't override the hitTest:withEvent: method), you won't get touch event.
If you set UIViewAnimationOptionAllowUserInteraction option, you can get touch event on the view's final frame (CGRectMake(-40, 200, 40, 40)), but in this example, the final frame is off the screen, you can set it to CGRectMake(0, 200, 40, 40) and have a test.

Hide keyboard by touching the screen

I want to hide the keyboard by touching the view. Everybody recommends to use this method, saying there's no need to link or anything else, but is not working.
The problem is that my method is not called.Is there anything else that should be done?
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[[self view] endEditing:YES];
}
I had trouble with this so use a method that loops through all views seeing if they are textviews and firstResponders. Not sure of the structure of a UITextView but you might need to check for that too although it may be covered.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UIView *txt in self.view.subviews){
if ([txt isKindOfClass:[UITextField class]] && [txt isFirstResponder]) {
[txt resignFirstResponder];
}
}
}
The best approach is to create a "lock view" which is a UIView that takes over the whole screen once the textField becomesFirstResponder. Make sure it's on top of all views (well, besides the textview, of course).
- (void)loadLockView {
CGRect bounds = [UIScreen mainScreen].bounds;
_lockView = [[UIView alloc] initWithFrame:bounds];
_lockView.backgroundColor = [UIColor clearColor];
UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(lockViewTapped:)];
[_lockView addGestureRecognizer:tgr];
[self.view addSubview:_lockView];
}
- (void)lockViewTapped:(UITapGestureRecognizer *)tgr {
[_lockView removeFromSuperView];
[_textField resignFirstResponder];
}
Use UITapGestureRecognizer For Dismiss keyboard.
Write this code on your viewdidload().
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(dismissKeyboard)];[self.view addGestureRecognizer:tap];
and in dismissKeyboard method put this code.
-(void)dismissKeyboard
{
[TextFieldName resignFirstResponder];
}

unable to add tap gesture to customized calloutView

I have a mapview in my app which needs customized calloutView for every map annotations.
Therefore, I have an XIB file for this customized calloutView.
Here is my code for the map view controller
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
CustomCalloutView *calloutView = (CustomCalloutView *)[[[NSBundle mainBundle] loadNibNamed:#"CustomCalloutView" owner:self options:nil] objectAtIndex:0];
[calloutView.layer setCornerRadius:10];
CGRect calloutViewFrame = calloutView.frame;
calloutViewFrame.origin = CGPointMake(-calloutViewFrame.size.width/2 + 15, -calloutViewFrame.size.height);
calloutView.frame = calloutViewFrame;
// some other code such as load images for calloutView
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap)];
singleTap.numberOfTapsRequired = 1;
[calloutView addGestureRecognizer:singleTap];
[view addSubview:calloutView];
}
- (void)handleSingleTap{
NSLog(#"it works");
}
However, the handleSingleTap: has never been called. Instead, every tap on the calloutView will only simply dismiss the calloutView. I also tried to add a button on the calloutView, but tap on it will also cause the calloutView dismissing, rather than calling the button action.
Can anyone help?
Update:
I've tried to change the code
[view addSubview:calloutView];
to
[self.view addSubview:calloutView];
which add the customized calloutView into the main container view rather than the mapView.
Then, it works fine with the tap gesture. Therefore, I think the problem should be caused by the mapView, it seems that mapView passes all the touch event on calloutView to itself. Anyone have ideas regarding this?
OK. I worked out the solution finally. We need to have a customized class for the MKAnnotationView to solve this. Here is the code in the .m file
- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent*)event
{
UIView* hitView = [super hitTest:point withEvent:event];
if (hitView != nil)
{
[self.superview bringSubviewToFront:self];
}
return hitView;
}
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent*)event
{
CGRect rect = self.bounds;
BOOL isInside = CGRectContainsPoint(rect, point);
if(!isInside)
{
for (UIView *view in self.subviews)
{
isInside = CGRectContainsPoint(view.frame, point);
if(isInside)
break;
}
}
return isInside;
}
Hopefully this can be helpful for others.
You need to modify your tapgesture implemenation & the method called.Have a look & check whether it work or not.
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap)];
singleTap.numberOfTapsRequired = 1;
[calloutView addGestureRecognizer:singleTap];
[view addSubview:calloutView];
- (void)handleSingleTap:(UIGestureRecognizer *)recognizer {
NSLog(#"it works");
}

UIButton as subview on complicated UIScrollView

In my app i implement really complicated control, which based on ARScrollViewEnhancer. I need it for displaying multiple pages of UIScrollView. So this is my code for UIScrollView:
dateScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(28, 0, dateLabelMaxSize, 45)];
dateScroll.pagingEnabled = YES;
dateScroll.clipsToBounds = NO;
dateScroll.showsHorizontalScrollIndicator = NO;
dateScroll.backgroundColor = [UIColor clearColor];
dateScroll.contentSize = CGSizeMake(dateLabelMaxSize*[dayArray count], 45);
I use dateScroll.clipsToBounds = NO; for displaying pages out of frame. This is code for ARScrollViewEnhancer:
ARScrollViewEnhancer *backForDate = [[ARScrollViewEnhancer alloc] initWithFrame:CGRectMake(0, 80, self.view.frame.size.width, 45)];
[backForDate addSubview:dateScroll];
backForDate._scrollView = dateScroll;
[self.view addSubview:backForDate];
Then i added subviews for UIScrollView. This is UILabels and UIButtons.
for (int i=0;i<[dayArray count];i++){
UILabel *dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(3+dateLabelMaxSize*i, 5, dateLabelMaxSize, 40)];
dateLabel.backgroundColor = [UIColor clearColor];
dateLabel.textAlignment = UITextAlignmentLeft;
dateLabel.text = #"some text...";
[dateScroll addSubview:dateLabel];
UIButton *dateButton = [UIButton buttonWithType:UIButtonTypeCustom];
//dateButton.backgroundColor = [UIColor redColor];
dateButton.frame = CGRectMake(3+dateLabelMaxSize*i, 5, dateLabelMaxSize-10, 40);
dateButton.tag = i;
[dateButton addTarget:self action:#selector(dateTapped:) forControlEvents:UIControlEventTouchUpInside];
[dateScroll addSubview:dateButton];
}
And method for buttons:
-(void)dateTapped:(UIButton*)button{
NSLog(#"%i",button.tag);
}
So, buttons not worked (( why?
Thnx.
UPD.
hitTest methode code. Was:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
if ([self pointInside:point withEvent:event]) {
return _scrollView;
}
return nil;
}
Now:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
UIView *scrv = _scrollView;
UIView *superView = [super hitTest:point withEvent:event];
if (superView == self)
{
CGPoint scrollViewpoint = [_scrollView convertPoint:point fromView:self];
for(UIView *view in _scrollView.subviews) {
if(CGRectContainsPoint(view.frame, scrollViewpoint)) {
return view;
}
}
return scrv;
}
return superView;
}
If you also copy the hitTest method in your custom control, it is the one causing the issue because it always return the scroll view instead of the button.
You need to return the correct subviews instead of always returning the subview, something like this:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
UIView *scrv = self.scrollView;
UIView *superView = [super hitTest:point withEvent:event];
if (superView == self)
{
CGPoint scrollViewpoint = [self.scrollView convertPoint:point fromView:self];
for(UIView *view in self.scrollView.subviews) {
if(CGRectContainsPoint(view.frame, scrollViewpoint)) {
return view;
}
}
return scrv;
}
return superView;
}

Passthrough Touch Event

What's the best way to passthrough a touch event to all subviews?
ViewController -> view -> (subview1, subview2)
I would like subview1 & subview2 to both respond to the touch event.
Set the tags on the subviews equal to noticable tags. Then do a tree search through the views looking for those tags. Unfortunately there is not really a nice way to do this without subclassing. If you were willing to subclass then you would subclass view and then upon touch throw a NSNotification that all other views of that same subclass would listen for.
In the parent's touch handler, you can iterate over the subviews of that view and call the same handler:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for(UIView *subview in [self.view subviews]) {
[subview touchesBegan:touches withEvent:event];
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
for(UIView *subview in [self.view subviews]) {
[subview touchesMoved:touches withEvent:event];
}
}
Or if you needed to identify specific subviews, you could assign integer Tags to the subviews to identify them later:
- (void)loadView {
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(10,10,10,10)];
view1.tag = 100;
[self.view addSubview:view1];
UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(20,20,20,20)];
view2.tag = 200;
[self.view addSubview:view2];
}
Then later in the ViewController method invoked by the touch event
- (void)touchEventResponder {
UIView *view1 = [self.view viewWithTag:100];
// Do work with view1
UIView *view2 = [self.view viewWithTag:200];
// Do work with view2
}

Resources