2015-08-28 180 views

回答

5

對不起,我太晚了,我只是意識到我可以幫助你。

你需要使用的是UIKeyCommand。檢查了這一點:http://nshipster.com/uikeycommand/

需要注意的是,檢測箭頭按鍵,你需要input: UIKeyInputLeftArrow而不是input: "j"時顯示出來(或當量)。你也不想修飾符(所以用戶不必按CMD-左箭頭鍵):請參考Key Commands with no modifier flags—Swift 2

基本上,你會想(外)您的viewDidLoad後沿此線的東西:

override func canBecomeFirstResponder() -> Bool { 
    return true 
} 

    override var keyCommands: [UIKeyCommand]? { 
    return [ 
     UIKeyCommand(input: UIKeyInputDownArrow, modifierFlags: [], action: "DownArrowPress:"), 
     UIKeyCommand(input: UIKeyInputUpArrow, modifierFlags: [], action: "UpArrowPress:"), 
     UIKeyCommand(input: " ", modifierFlags: [], action: "SpaceKeyPress:")] 
    } 

// ... 

func DownArrowPress(sender: UIKeyCommand) { 
// this happens when you press the down arrow. 
} 
func UpArrowPress(sender: UIKeyCommand) { 
// this happens when you press the up arrow. 
} 
func SpaceKeyPress(sender: UIKeyCommand) { 
// this happens when you press the space key. 
} 

我希望這可以幫助,如果你需要更多的幫助,或發表評論的東西回來@owlswipe是不正確的。