Swi*_*bit 14 uitextview uigesturerecognizer ios swift
我一直在努力用Swift检测UITextView上的点击.
我的UITextViews在一个表中,我必须能够检测链接并按下它们,这些链接长度未知.
此外,如果我点击单元格,我没有点击链接,我想在我的导航控制器堆栈上推送一个新的UIViewController.
我试图创建自己的textview来覆盖touchesCancelled,但这并不成功.它检测到在实际设备上不被视为点击的取消.
该模拟器中不会出现该错误,但似乎我无法点击真实设备,只需长按即可.
class LinkTextView: UITextView {
override func touchesCancelled(touches: Set<NSObject>!, withEvent event: UIEvent!) {
self.textViewWasTapped(self)
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试直接添加手势识别器.我也没有任何成功.它根本不会调用手势识别器.
我将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)
Run Code Online (Sandbox Code Playgroud)
在我的LinkTextView类中:
class LinkTextView: UITextView {
func tapTextView(stapGesture: UIGestureRecognizer){
println("TAPPING1")
}
}
Run Code Online (Sandbox Code Playgroud)
我看了论坛,发现这篇文章:发帖.它建议使用CHHLinkTextView.我尝试使用它,但我想要的是自动检测链接,正常的uitextview实际上是这样.
我尝试使用界面构建器中的checkBox来解析CHHLinkTextView的链接,但它不起作用.我没有在文档中看到任何暗示它可以完成的内容.
我该怎么办?
您在第一次尝试子类化时非常接近UITextView,但不是仅仅覆盖,而是touchesCancelled要覆盖所有touches*方法,即
touchesBegantouchesMovedtouchesEndedtouchesCancelled在重写的方法中,通过获取textView来向下发送响应者链nextResponder(),检查它是否是nil,并在下一个响应者上调用该方法.
这样做的原因是因为UITextView如果它在URL上就会拦截触摸并且UIResponder永远不会调用这些方法 - 通过textView:shouldInteractWithURL:inRange在tableViewCell中或者在任何地方实现它来自己尝试,并且你会看到它在任何触摸事件之前被调用传了过来.
这应该是你尝试做的最小值(它是重复但很短):
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
}
}
Run Code Online (Sandbox Code Playgroud)
我做了,因为Ralfonso说没有成功,但它帮助我意识到我有一个手势识别器冲突.事实证明我没有覆盖UITextView的所有4种方法,我只是覆盖了touchesBegan.我在viewDidLoad中做的是添加一个手势识别器来关闭键盘:
var tapOutTextField: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
self.view.addGestureRecognizer(tapOutTextField)
Run Code Online (Sandbox Code Playgroud)
什么没有意义,并把我送错了方向是touchesBegan实际上是在延迟后调用的(所以touchCancelled),我无法获得与UIButton相同的行为.所有这一切都是因为这种手势识别器冲突.
| 归档时间: |
|
| 查看次数: |
10786 次 |
| 最近记录: |