如何解决SWIFT:当我触摸屏幕时touchesEnded没有被调用
因此,我有一个名为SettingsViewController的tableviewController,它具有以下touchesEnded函数:
override func touchesEnded(_ touches: Set<UITouch>,with event: UIEvent?) {
print("A")
super.touchesEnded(touches,with: event)
print("B")
if let touch = touches.first {
print("C")
let touchLocation = touch.location(in: view)
// 290 because the width of the view is 414,and the SettingsViewController width gets set to 0.7 * the view width in SlideInTransition. 0.7 * 414 is 289.8
if touchLocation.x > 290 {
dismiss(animated: true)
}
}
}
我做了打印语句,看它是否被调用,不是。该视图控制器在自定义过渡中带有“菜单式”幻灯片。出现自定义转换的新tablevc的UIView的边界是否可能是问题所在?无论如何,我都尝试实现了super.touchesEnded,或者添加了所有覆盖触摸功能,并在每次模拟时在屏幕上进行了轻触,但从未调用过touchesEnded。知道发生了什么问题以及如何解决吗?谢谢
解决方法
我猜您正在将touchesEnded()
函数放入表视图控制器中 ...这将无法正常工作。表格视图本身正在消耗这些触摸。
这可能对您有用...
将此UITableView
子类添加到您的项目中:
class TouchableTableView: UITableView {
var callback: (() -> ())?
override func touchesEnded(_ touches: Set<UITouch>,with event: UIEvent?) {
print("A")
super.touchesEnded(touches,with: event)
print("B")
if let touch = touches.first {
print("C")
let touchLocation = touch.location(in: self)
// not clear where you're trying to check for touch,so this may need to be changed
// 290 because the width of the view is 414,and the SettingsViewController width gets set to 0.7 * the view width in SlideInTransition. 0.7 * 414 is 289.8
if touchLocation.x > 290 {
// this code is in a UITableView subclass,// so we can't call dismiss() from here
//dismiss(animated: true)
// instead,we tell the Controller to take action
callback?()
}
}
}
}
然后,在情节提要中,在Table View Controller中选择Table View并将其自定义类分配给TouchableTableView
:
现在,touchesEnded()
将在自定义TouchableTableView
类中被调用(除非您点击单元格中的交互式UI元素,例如按钮)。
然后,我们使用“回调”闭包告诉控制器有关触摸的信息。因此,在您的UITableViewController
中,将其添加到viewDidLoad()
中:
class ExampleTableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
// we're in a UITableViewController,// so make sure the tableView is our TouchableTableView class
// if it is,setup the closure
if let tv = tableView as? TouchableTableView {
tv.callback = {
// here is where we would,for example,call dismiss()
self.dismiss(animated: true,completion: nil)
}
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。