我有一个类似于View 1的视图,它有一个UITextView子视图。
我必须在我的textview中启用链接和日期事件,我已在其上启用了用户交互。但是我需要在它的超级视图上进行一些轻击处理,即视图1,所以我在超级视图上也启用了轻击手势。
使用此设置,我将在文本视图中获得链接检测和处理,如果我没有点击链接,则点按手势将自动传递到超级视图。然而,在iOS 11中,这似乎被破坏了,我似乎永远不会将轻击手势传递给超级视图。有人可以帮我在iOS 11上恢复与以前版本相同的行为。
我写的代码是这样的 -
[V addSubView:tv];
[tv setFrame:[self calculateFrameForTextView]];
[tv setUserInteractionEnabled:YES];
[tv setEditable:NO];
[tv setSelectable:YES];
[tv setDataDetectorTypes:UIDataDetectorTypeAll];
UITapGestureRecognizer *tap= [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doSomething)];
[V addGestureRecognizer:tap];
............................
| UIView (V) |
| |
| |-------------------| |
| | UITextView (tv) | |
| |...................| |
.............................
如果我点击文本视图中不是链接/其他数据检测器类型的任何内容,我希望将“doSomething”调用为在超级视图上点击注册的选择器。此行为在版本< iOS11
答案 0 :(得分:0)
我终于找到了一种从iOS 11中的文本视图检测触摸的解决方案,尚未在较早版本上进行测试。
点击链接或附件不会触发关闭。
/// Provide a callback when single tapping on a non-attachment area
@IBDesignable class UITextViewFixed: UITextView {
var didTappedOnAreaBesidesAttachment: (() -> ())? = nil
// A single tap won't move.
private var isTouchMoved = false
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesMoved(touches, with: event)
isTouchMoved = true
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesEnded(touches, with: event)
if !isTouchMoved &&
!isTapOnAttachment(touches.first!.location(in: self)) &&
selectedRange.length == 0 {
didTappedOnAreaBesidesAttachment?()
}
isTouchMoved = false
}
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesCancelled(touches, with: event)
// `UITextView` will cancel the touch then starting selection
isTouchMoved = false
}
private func isTapOnAttachment(_ point: CGPoint) -> Bool {
let range = NSRange(location: 0, length: attributedText.length)
var found = false
attributedText.enumerateAttribute(.attachment, in: range, options: []) { (value, effectiveRange, stop) in
guard value is NSTextAttachment else {
return
}
let rect = layoutManager.boundingRect(forGlyphRange: effectiveRange, in: textContainer)
if rect.contains(point) {
found = true
stop.pointee = true
}
}
return found
}
}