我知道这已被问及之前,我看到的唯一答案是“不需要外接键盘,因为它违背了UI准则”。但是,我想使用这样的脚踏板:http://www.bilila.com/page_turner_for_ipad在我的应用程序中的页面之间切换(除了滑动)。此页面翻转器模拟键盘并使用向上/向下箭头键。如何响应外部键盘箭头键?
所以这里是我的问题:我如何回应这些箭头键事件?它必须是可能的,因为其他应用程序管理,但我画空白。
我知道这已被问及之前,我看到的唯一答案是“不需要外接键盘,因为它违背了UI准则”。但是,我想使用这样的脚踏板:http://www.bilila.com/page_turner_for_ipad在我的应用程序中的页面之间切换(除了滑动)。此页面翻转器模拟键盘并使用向上/向下箭头键。如何响应外部键盘箭头键?
所以这里是我的问题:我如何回应这些箭头键事件?它必须是可能的,因为其他应用程序管理,但我画空白。
对于那些在iOS 7下寻找解决方案的用户,有一个名为keyCommands的新UIResponder属性。创建UITextView的子类并执行如下的keyCommands ...
@implementation ArrowKeyTextView
- (id) initWithFrame: (CGRect) frame
{
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
- (NSArray *) keyCommands
{
UIKeyCommand *upArrow = [UIKeyCommand keyCommandWithInput: UIKeyInputUpArrow modifierFlags: 0 action: @selector(upArrow:)];
UIKeyCommand *downArrow = [UIKeyCommand keyCommandWithInput: UIKeyInputDownArrow modifierFlags: 0 action: @selector(downArrow:)];
UIKeyCommand *leftArrow = [UIKeyCommand keyCommandWithInput: UIKeyInputLeftArrow modifierFlags: 0 action: @selector(leftArrow:)];
UIKeyCommand *rightArrow = [UIKeyCommand keyCommandWithInput: UIKeyInputRightArrow modifierFlags: 0 action: @selector(rightArrow:)];
return [[NSArray alloc] initWithObjects: upArrow, downArrow, leftArrow, rightArrow, nil];
}
- (void) upArrow: (UIKeyCommand *) keyCommand
{
}
- (void) downArrow: (UIKeyCommand *) keyCommand
{
}
- (void) leftArrow: (UIKeyCommand *) keyCommand
{
}
- (void) rightArrow: (UIKeyCommand *) keyCommand
{
}
Sorted!我简单地使用一个1x1px文本查看和使用委托方法textViewDidChangeSelection:
编辑:对于iOS 6我不得不改变文本视图到50x50px(或至少足以实际显示文本)这个工作
当踏板断开连接时,我也设法抑制了屏幕键盘。
这是我的代码在viewDidLoad中:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillAppear:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillDisappear:) name:UIKeyboardWillHideNotification object:nil];
UITextView *hiddenTextView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
[hiddenTextView setHidden:YES];
hiddenTextView.text = @"aa";
hiddenTextView.delegate = self;
hiddenTextView.selectedRange = NSMakeRange(1, 0);
[self.view addSubview:hiddenTextView];
[hiddenTextView becomeFirstResponder];
if (keyboardShown)
[hiddenTextView resignFirstResponder];
keyboardShown
被声明为我的头文件bool
。
然后将这些方法:
- (void)textViewDidChangeSelection:(UITextView *)textView {
/******TEXT FIELD CARET CHANGED******/
if (textView.selectedRange.location == 2) {
// End of text - down arrow pressed
textView.selectedRange = NSMakeRange(1, 0);
} else if (textView.selectedRange.location == 0) {
// Beginning of text - up arrow pressed
textView.selectedRange = NSMakeRange(1, 0);
}
// Check if text has changed and replace with original
if (![textView.text isEqualToString:@"aa"])
textView.text = @"aa";
}
- (void)keyboardWillAppear:(NSNotification *)aNotification {
keyboardShown = YES;
}
- (void)keyboardWillDisappear:(NSNotification *)aNotification {
keyboardShown = NO;
}
我希望这个代码可以帮助别人谁是寻找一个解决这个问题。随意使用它。
对我很好,但我不得不把初始化代码放在viewDidAppear而不是viewDidLoad中。 – Bryan
我认为这会导致无限循环,因为设置'selectedRange'属性(在'textViewDidChangeSelection'中)会触发另一个'textViewDidChangeSelection',尽管这个想法对我确实有帮助。 –
它不会导致无限循环,因为'selectedRange'只在光标位置为0或2时设置。 – colincameron
它可以与'UIWebView'一起使用吗? – Dmitry
@Altaveron keyCommands属性是UIResponder上的一个属性,UIWebView继承自UIResponder,所以我可以看到它没有理由不起作用。 – amergin
'UIWebView'是许多子视图的容器。 – Dmitry