Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forward touches other than double tap to UITextView

I have a UITextView on top of that i have multiple UIView. Any touch action on UIView can associated to corresponding IBAction. In my scenario double tap gesture need to be associated to UIView's IBAction, any other touch apart from this needs to be forwarded to the underlying UITextView.

My code block of doing the above scenario

UITextView *textView = [[UITextView alloc] init];

UIView *subview = [[UIView alloc] init];

UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDetected:)];
doubleTap.numberOfTapsRequired = 2;
[subview addGestureRecognizer:doubleTap];

[textView addSubview: subview];

I have tried the following SO solutions,

How to ignore touch events and pass them to another subview's UIControl objects?

using hitTest i'm unable to capture double tap gesture in UIViews which is present on top of UITextView

Make UIGestureRecognizer to receive touches started on subviews

forwarding UITouch set from touchBegan:withEvent: in UIView to UITextView is not giving expected default behavior of UITextView

Is there any other solutions available to address this scenario?

Thanks in advance.

like image 593
The Debugger Avatar asked Oct 21 '22 21:10

The Debugger


1 Answers

This could be accomplished by:

  1. Letting UITextView handle the touch events as suggested in How to ignore touch events and pass them to another subview's UIControl objects?
  2. In the hitTest of UIView save the reference to the view in which it occurred so that it could be used later
  3. Add a custom double tap gesture recognizer, that checks if the hitTest reference is not null, indicating that the double tap happened on the target view and forward the event to that view. If not, fail the gesture recognizer. To fail the gesture recognizer you need to include UIKit/UIGestureRecognizerSubclass.h and set the state property to UIGestureRecognizerStateFailed
  4. Add the gesture recognizer to the UITextView
  5. Extend the UITextView to override the gestureRecognizers getter and loop through the gesture recognizers to require the custom gesture to fail. This has to be done in the getter as the gesturerecognizers seem to get reset often
like image 103
Krishna Avatar answered Oct 27 '22 09:10

Krishna