【发布时间】:2017-06-03 12:59:22
【问题描述】:
如果注册了一个按键命令,如果用户按住按键时间过长,它的动作可能会被多次调用。这会产生非常奇怪的效果,例如 ⌘N 可以多次重复打开新视图。是否有任何简单的方法来阻止这种行为,而无需求助于诸如布尔“已触发”标志之类的东西?
以下是我注册两个不同按键命令的方法:
#pragma mark - KeyCommands
- (BOOL)canBecomeFirstResponder {
return YES;
}
- (NSArray<UIKeyCommand *>*)keyCommands {
return @[
[UIKeyCommand keyCommandWithInput:@"O" modifierFlags:UIKeyModifierCommand action:@selector(keyboardShowOtherView:) discoverabilityTitle:@"Show Other View"],
[UIKeyCommand keyCommandWithInput:@"S" modifierFlags:UIKeyModifierCommand action:@selector(keyboardPlaySound:) discoverabilityTitle:@"Play Sound"],
];
}
- (void)keyboardShowOtherView:(UIKeyCommand *)sender {
NSLog(@"keyboardShowOtherView");
[self performSegueWithIdentifier:@"showOtherView" sender:nil];
}
- (void)keyboardPlaySound:(UIKeyCommand *)sender {
NSLog(@"keyboardPlaySound");
[self playSound:sender];
}
#pragma mark - Actions
- (IBAction)playSound:(id)sender {
AudioServicesPlaySystemSound(1006); // Not allowed in the AppStore
}
示例项目可以在这里下载:TestKeyCommands.zip
【问题讨论】:
标签: ios iphone ipad uikeycommand