检测UITextView上的点按仅检测取消[重复]

问题描述 投票:13回答:2

我一直在努力用Swift检测UITextView上的点击。

我的UITextViews在一个表中,我必须能够检测链接并按下它们,这些链接长度未知。

此外,如果我点击单元格,我没有点击链接,我想在我的导航控制器堆栈上推送一个新的UIViewController。

我试图创建自己的textview来覆盖touchesCancelled,但这并不成功。它检测到在实际设备上不被视为点击的取消。

该模拟器中不会出现该错误,但似乎我无法点击真实设备,只需长按即可。

class LinkTextView: UITextView {
    override func touchesCancelled(touches: Set<NSObject>!, withEvent event: UIEvent!) {
         self.textViewWasTapped(self) 
    }
}

我尝试直接添加手势识别器。我也没有任何成功。它根本不会调用手势识别器。

我将UIGestureReconizer委托添加到我的UIViewController和我的那些行中

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
     var singleTap : UIGestureRecognizer = UIGestureRecognizer(target: self, action: "tapTextView:")
        singleTap.delegate = self
             cell.mTextOutlet.attributedText = myMutableString
             cell.mTextOutlet.addGestureRecognizer(singleTap) 

在我的LinkTextView类中:

class LinkTextView: UITextView {
    func tapTextView(stapGesture: UIGestureRecognizer){
        println("TAPPING1")
    }
}

我看了论坛,发现这篇文章:post。它建议使用CHHLinkTextView。我尝试使用它,但我想要的是自动检测链接,正常的uitextview实际上是这样。

我尝试使用界面构建器中的checkBox来解析CHHLinkTextView的链接,但它不起作用。我没有在文档中看到任何暗示它可以完成的内容。

我该怎么办?

ios swift uitextview uigesturerecognizer
2个回答
9
投票

你在第一次尝试子类化UITextView时非常接近,但是你不想覆盖touchesCancelled,而是要覆盖所有的touches*方法,即

  • touchesBegan
  • touchesMoved
  • touchesEnded
  • touchesCancelled

在重写的方法中,通过获取textView的nextResponder()向下发送响应链,检查它不是nil,并在下一个响应者上调用该方法。

这个工作的原因是因为UITextView将拦截触摸,如果它在URL上并且UIResponder方法永远不会被调用 - 通过在tableViewCell中或在任何地方实现textView:shouldInteractWithURL:inRange来自己尝试,并且你会看到它在任何之前被调用触摸事件传递。

这应该是你尝试做的最小值(它是重复但很短):

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
    }

}

6
投票

我做了,因为Ralfonso说没有成功,但它帮助我意识到我有一个手势识别器冲突。事实证明我没有覆盖UITextView的所有4种方法,我只是覆盖了touchesBegan。我在viewDidLoad中做的是添加一个手势识别器来关闭键盘:

      var tapOutTextField: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
      self.view.addGestureRecognizer(tapOutTextField)

什么没有意义,并把我送错了方向是touchesBegan实际上是在延迟后调用的(所以touchCancelled),我无法获得与UIButton相同的行为。所有这一切都是因为这种手势识别器冲突。

© www.soinside.com 2019 - 2024. All rights reserved.