![](/img/trans.png)
[英]How can I support the arrow keys on UIWebView with a Bluetooth keyboard under?
[英]How can I respond to external keyboard arrow keys?
我知道之前已經問過,我見過的唯一答案是“不需要外部鍵盤,因為它違反了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
{
}
排序! 我只是使用1x1px文本視圖並使用委托方法textViewDidChangeSelection 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;
}
我希望這段代碼可以幫助那些正在尋找解決這個問題的人。 隨意使用它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.