我一直在努力使用Swift检测UITextView上的轻击。
我的UITextView在一个表格中,我必须能够检测链接并按下它们,这些链接的长度是未知的。
此外,如果我点击了单元格而不是链接,我希望将一个新的UIViewController推入我的导航控制器堆栈。
我尝试创建自己的文本视图以覆盖touchesCancelled,但没有成功。它会检测到取消操作,但在真实设备上却不被认为是轻击。
这个bug在模拟器中不会出现,但似乎我无法在真实设备上轻击,只有长按才有效。
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。它建议使用CCHLinkTextView。我尝试使用它,但我想要的是自动检测链接,而普通的UITextView确实可以做到。
我尝试使用接口构建器中的复选框来解析CHHLinkTextView中的链接,但它不起作用。我在文档中没有看到任何建议可以完成此操作的内容。
我该如何继续进行?
next
来自哪里? - Andrew Kirnanext
是UIResponder
的成员:https://developer.apple.com/documentation/uikit/uiresponder - Ralfonso