我知道之前已经问过这个问题,我见过的唯一答案是“不需要外部键盘,因为它违反了UI指南”。但是,我想使用这样的脚踏板:http://www.bilila.com/page_turner_for_ipad来改变我的应用程序中的页面(除了滑动)。此页面调整器模拟键盘并使用向上/向下箭头键。
所以这是我的问题:我如何回应这些箭头键事件?它必须可以像其他应用程序一样管理,但我要画一个空白。
答案 0 :(得分:26)
对于那些在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
{
}
答案 1 :(得分:9)
排序!我只是使用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;
}
我希望此代码可以帮助正在寻找此问题解决方案的其他人。随意使用它。