質問

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.

役に立ちましたか?

解決

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

他のヒント

The subview is unnecessary, remove it, and replace:

[subview addGestureRecognizer:doubleTap];

WITH:

[textView addGestureRecognizer:doubleTap];

Now you should get your event in tapDetected: method.

To make sure you are not changing UITextView behaviour, use this methods:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;

OR

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top