2011-11-02 69 views
11

我知道這已被問及之前,我看到的唯一答案是「不需要外接鍵盤,因爲它違背了UI準則」。但是,我想使用這樣的腳踏板:http://www.bilila.com/page_turner_for_ipad在我的應用程序中的頁面之間切換(除了滑動)。此頁面翻轉器模擬鍵盤並使用向上/向下箭頭鍵。如何響應外部鍵盤箭頭鍵?

所以這裏是我的問題:我如何迴應這些箭頭鍵事件?它必須是可能的,因爲其他應用程序管理,但我畫空白。

回答

21

對於那些在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 
{ 

} 
+0

它可以與'UIWebView'一起使用嗎? – Dmitry

+0

@Altaveron keyCommands屬性是UIResponder上的一個屬性,UIWebView繼承自UIResponder,所以我可以看到它沒有理由不起作用。 – amergin

+0

'UIWebView'是許多子視圖的容器。 – Dmitry

9

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

我希望這個代碼可以幫助別人誰是尋找一個解決這個問題。隨意使用它。

+1

對我很好,但我不得不把初始化代碼放在viewDidAppear而不是viewDidLoad中。 – Bryan

+2

我認爲這會導致無限循環,因爲設置'selectedRange'屬性(在'textViewDidChangeSelection'中)會觸發另一個'textViewDidChangeSelection',儘管這個想法對我確實有幫助。 –

+0

它不會導致無限循環,因爲'selectedRange'只在光標位置爲0或2時設置。 – colincameron