Ios 子类化UIView时如何检测箭头键?
基本上,当按下其中一个箭头键时,我想做一些事情 我读过很多不同的问题。他们中的许多人谈到了Ios 子类化UIView时如何检测箭头键?,ios,swift,uiview,keyboard,subclass,Ios,Swift,Uiview,Keyboard,Subclass,基本上,当按下其中一个箭头键时,我想做一些事情 我读过很多不同的问题。他们中的许多人谈到了keyDown,但这是针对NSViewController或NSWindow(和(苹果文档))。当我用这个的时候,我以为我发现了什么: func setKeys() { let up = UIKeyCommand(input: UIKeyCommand.inputUpArrow, modifierFlags: [], action: #selector(upPressed)) } @objc fu
keyDown
,但这是针对NSViewController
或NSWindow
(和(苹果文档))。当我用这个的时候,我以为我发现了什么:
func setKeys() {
let up = UIKeyCommand(input: UIKeyCommand.inputUpArrow, modifierFlags: [], action: #selector(upPressed))
}
@objc func upPressed() {
print("Hello")
}
但是,甚至没有调用
upPressed()
。实现这一点的最佳方法是什么?您没有使用返回的UIKeyCommand实例up
Apple:“创建key command对象后,您可以使用视图控制器的addKeyCommand:方法将其添加到视图控制器。您还可以覆盖任何响应程序类,并直接从响应程序的keyCommands属性返回key命令。”
使用模拟器和硬件键盘对此进行了测试
此外:如果要通过UIView直接实现它,则必须执行:“……还可以重写任何响应程序类,并直接从响应程序的keyCommands属性返回key命令。”因为UIView符合UIResponder
class CustomView: UIView{
override var keyCommands: [UIKeyCommand]? {
return [UIKeyCommand(input: UIKeyCommand.inputUpArrow, modifierFlags: [], action: #selector(upPressed))]
}
@objc func upPressed(){
print("hello world")
}
}
class CustomView: UIView{
override var keyCommands: [UIKeyCommand]? {
return [UIKeyCommand(input: UIKeyCommand.inputUpArrow, modifierFlags: [], action: #selector(upPressed))]
}
@objc func upPressed(){
print("hello world")
}
}