gpt4 book ai didi

objective-c - 将除双击以外的触摸转发到 UITextView

转载 作者:可可西里 更新时间:2023-11-01 04:44:26 30 4
gpt4 key购买 nike

我有一个 UITextView 在上面我有多个 UIViewUIView 上的任何触摸操作都可以关联到相应的 IBAction。在我的场景中,双击手势需要关联到 UIViewIBAction,除此之外的任何其他触摸都需要转发到底层 UITextView.

我执行上述场景的代码块

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];

我尝试了以下 SO 解决方案,

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

使用 hitTest 我无法在位于 UITextView 顶部的 UIViews 中捕获双击手势

Make UIGestureRecognizer to receive touches started on subviews

UIView 中的 touchBegan:withEvent: 设置的 UITouch 转发到 UITextView 未给出预期的默认行为UITextView

是否有任何其他解决方案可用于解决这种情况?

提前致谢。

最佳答案

这可以通过以下方式实现:

  1. 让 UITextView 按照 How to ignore touch events and pass them to another subview's UIControl objects? 中的建议处理触摸事件
  2. 在 UIView 的 hitTest 中保存对发生它的 View 的引用,以便以后使用
  3. 添加自定义双击手势识别器,检查 hitTest 引用是否不为 null,指示双击发生在目标 View 上并将事件转发到该 View 。如果不是,则使手势识别器失败。要使手势识别器失败,您需要包含 UIKit/UIGestureRecognizerSubclass.h 并将状态属性设置为 UIGestureRecognizerStateFailed
  4. 将手势识别器添加到 UITextView
  5. 扩展 UITextView 以覆盖 gestureRecognizers getter 并循环遍历手势识别器以要求自定义手势失败。这必须在 setter/getter 中完成,因为手势识别器似乎经常被重置

关于objective-c - 将除双击以外的触摸转发到 UITextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14050994/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com