Ios 检测UITextView上的点击仅检测取消
我一直在努力用Swift检测UITextView上的点击 我的UITextView在一个表中,我必须能够检测链接并按下它们,这些链接的长度是未知的 此外,如果我点击单元格,而不是点击链接,我希望在导航控制器堆栈上推送一个新的UIViewController 我试图创建自己的文本视图来覆盖touchesCancelled,但没有成功。它检测到取消,而这并不被认为是对真实设备的点击 这个错误并没有出现在模拟器中,但我似乎无法点击真实的设备,只有长按才能工作Ios 检测UITextView上的点击仅检测取消,ios,swift,uitextview,uigesturerecognizer,Ios,Swift,Uitextview,Uigesturerecognizer,我一直在努力用Swift检测UITextView上的点击 我的UITextView在一个表中,我必须能够检测链接并按下它们,这些链接的长度是未知的 此外,如果我点击单元格,而不是点击链接,我希望在导航控制器堆栈上推送一个新的UIViewController 我试图创建自己的文本视图来覆盖touchesCancelled,但没有成功。它检测到取消,而这并不被认为是对真实设备的点击 这个错误并没有出现在模拟器中,但我似乎无法点击真实的设备,只有长按才能工作 class LinkTextView: U
class LinkTextView: UITextView {
override func touchesCancelled(touches: Set<NSObject>!, withEvent event: UIEvent!) {
self.textViewWasTapped(self)
}
}
在我的LinkTextView类中:
class LinkTextView: UITextView {
func tapTextView(stapGesture: UIGestureRecognizer){
println("TAPPING1")
}
}
我查看了论坛,发现了以下帖子:。它建议使用。我试着使用它,但我想要的是自动检测链接,而正常的uitextview实际上就是这样做的
我确实尝试过使用interface builder中的复选框来解析带有CHHLinkTextView的链接,但不起作用。我在文档中没有看到任何提示可以这样做的内容
我应该如何继续?您第一次尝试子类化
UITextView
,已经非常接近了,但是您需要覆盖所有touchscancelled
方法,而不是只覆盖touchscancelled
触摸开始
触摸移动
touchesend
touchscancelled
nextResponder()
,将触碰发送到响应者链,检查它是否为nil
,并在下一个响应者上调用该方法
这样做之所以有效,是因为如果触摸位于URL上,则UITextView
将拦截触摸,并且永远不会调用UIResponder
方法——通过在tableViewCell或任何地方实现textView:shouldInteractWithURL:inRange
自己尝试一下,你会看到它在任何触摸事件传递之前被调用
这应该是您尝试做的最低要求(重复但简短):
类PassThroughTextView:UITextView{
覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){
如果让下一个=下一个{
next.touchesbeated(touches,with:event)
}否则{
super.touchesbeated(touches,with:event)
}
}
覆盖函数touchesend(touchs:Set,带有事件:UIEvent?){
如果让下一个=下一个{
next.touchesend(touchs,with:event)
}否则{
super.touchesend(触摸,带有:事件)
}
}
覆盖功能触摸已取消(touchs:Set,带有事件:UIEvent?){
如果让下一个=下一个{
next.touchscancelled(touchs,with:event)
}否则{
super.touchscancelled(触摸,带有:事件)
}
}
覆盖功能触摸移动(touchs:Set,带有事件:UIEvent?){
如果让下一个=下一个{
next.touchsmoved(touchs,with:event)
}否则{
super.touchesMoved(touches,with:event)
}
}
}
//如果您想对tableViewCell中的URL或textView执行任何操作。。。
类TableViewCell:UITableViewCell、UITextViewDelegate{
@IBOutlet var textView:PassThroughTextView!
func textView(textView:UITextView,应与URL:NSURL、inRange characterRange:NSRange)交互)->Bool{
println(“文本视图应与URL交互”)
返回真值
}
}
我按照拉尔丰索说的做了,但没有成功,但这让我意识到我遇到了手势识别器冲突。事实证明,我不必覆盖UITextView的所有4种方法,我只需覆盖ToucheSStart。
我在viewDidLoad中所做的是添加一个手势识别器来关闭键盘:
var tapOutTextField: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
self.view.addGestureRecognizer(tapOutTextField)
不合理的是,touchesbearth实际上是在延迟后调用的(touchescelected也是如此),我无法获得与UIButton相同的行为。所有这些都是因为这个手势识别器冲突。很高兴你能解决它!手势识别器可能看起来有点奇怪,因为在研究你的问题之前,我没有意识到先发制人的链接交互,所以直到。但是要记住一件事,如果你不是在
触摸开始:
中调用super
,文档说你还必须覆盖其他触摸处理方法::这可能是避免以后出现奇怪行为的一个好主意。下一步
来自哪里?我猜它是作为某种超类继承的(可能是NSObject或UIView)。谢谢你的简单回答。这对我很有效:]我的UICollectionViewCell带有嵌套UITextView,现在可以将点击传递到它们所属的单元格以执行翻转动画!很抱歉延迟,但为了子孙后代,next
是UIResponder
的成员:
class PassThroughTextView: UITextView {
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let next = next {
next.touchesBegan(touches, with: event)
} else {
super.touchesBegan(touches, with: event)
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if let next = next {
next.touchesEnded(touches, with: event)
} else {
super.touchesEnded(touches, with: event)
}
}
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
if let next = next {
next.touchesCancelled(touches, with: event)
} else {
super.touchesCancelled(touches, with: event)
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
if let next = next {
next.touchesMoved(touches, with: event)
} else {
super.touchesMoved(touches, with: event)
}
}
}
// In case you want to do anything with the URL or textView in your tableViewCell...
class TableViewCell: UITableViewCell, UITextViewDelegate {
@IBOutlet var textView: PassThroughTextView!
func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool {
println("textView shouldInteractWithURL")
return true
}
}
var tapOutTextField: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
self.view.addGestureRecognizer(tapOutTextField)