SWIFT: touchesEnded 在我触摸屏幕时没有被调用
SWIFT: touchesEnded not being called when I touch the screen
所以我有一个名为 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)
}
}
}
我制作了 print 语句以查看它是否被调用,但事实并非如此。此视图控制器在自定义过渡中带有 'menu-esque' 幻灯片。是否有可能是用自定义转换显示的新 tablevc 的 UIView 的边界是某种问题?无论如何,我已经尝试实现 super.touchesEnded,或添加所有覆盖触摸功能并在每次模拟时在整个屏幕上点击,但从未调用 touchesEnded。知道出了什么问题以及如何解决吗?谢谢
我猜你要把 touchesEnded()
func 放在你的 table view controller ...工作。 table 视图本身正在消耗触摸。
这可能对你有用...
将这个 UITableView
subclass 添加到您的项目中:
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?()
}
}
}
}
然后,在 Storyboard 中,select Table View in your Table View Controller 并将其 Custom Class 分配给 TouchableTableView
:
现在,touchesEnded()
将在自定义 TouchableTableView
class 中调用(除非您点击单元格中的交互式 UI 元素,例如按钮) .
然后我们使用“回调”闭包来告诉 Controller 有关触摸的信息。因此,在您的 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)
}
}
}
}
所以我有一个名为 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)
}
}
}
我制作了 print 语句以查看它是否被调用,但事实并非如此。此视图控制器在自定义过渡中带有 'menu-esque' 幻灯片。是否有可能是用自定义转换显示的新 tablevc 的 UIView 的边界是某种问题?无论如何,我已经尝试实现 super.touchesEnded,或添加所有覆盖触摸功能并在每次模拟时在整个屏幕上点击,但从未调用 touchesEnded。知道出了什么问题以及如何解决吗?谢谢
我猜你要把 touchesEnded()
func 放在你的 table view controller ...工作。 table 视图本身正在消耗触摸。
这可能对你有用...
将这个 UITableView
subclass 添加到您的项目中:
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?()
}
}
}
}
然后,在 Storyboard 中,select Table View in your Table View Controller 并将其 Custom Class 分配给 TouchableTableView
:
现在,touchesEnded()
将在自定义 TouchableTableView
class 中调用(除非您点击单元格中的交互式 UI 元素,例如按钮) .
然后我们使用“回调”闭包来告诉 Controller 有关触摸的信息。因此,在您的 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)
}
}
}
}