12

我知道以前有人问过这个问题,我看到的唯一答案是“不需要外部键盘,因为它违反了 UI 准则”。但是,我想使用这样的脚踏板:http: //www.bilila.com/page_turner_for_ipad在我的应用程序中的页面之间切换(除了滑动)。此翻页器模拟键盘并使用向上/向下箭头键。

所以这是我的问题:我如何响应这些箭头键事件?其他应用程序管理它必须是可能的,但我正在画一个空白。

4

2 回答 2

28

对于那些在 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
{

}
于 2013-10-26T14:37:39.570 回答
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];

keyboardShownbool在我的头文件中声明为 a 。

然后添加这些方法:

- (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;
}

我希望这段代码可以帮助正在寻找解决此问题的其他人。随意使用它。

于 2011-11-07T12:29:20.993 回答