2011-11-02 10 views
11

So che è stato chiesto prima e le uniche risposte che ho visto sono "Non richiedono una tastiera esterna, in quanto va contro le linee guida dell'interfaccia utente". Tuttavia, voglio usare un pedale come questo: http://www.bilila.com/page_turner_for_ipad per cambiare tra le pagine nella mia app (oltre a scorrere). Questo page turner emula una tastiera e utilizza i tasti freccia su/giù.Come posso rispondere ai tasti freccia della tastiera esterna?

Quindi, ecco la mia domanda: come posso rispondere a questi eventi con i tasti freccia? Deve essere possibile mentre altre app gestiscono, ma sto disegnando un vuoto.

risposta

21

Per coloro che sono alla ricerca di una soluzione con iOS 7, esiste una nuova proprietà UIResponder chiamata keyCommands. Creare una sottoclasse di UITextView e implementare keyCommands come segue ...

@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

Funzionerà con 'UIWebView'? – Dmitry

+0

@Altaveron la proprietà keyCommands è una proprietà su UIResponder e UIWebView eredita da UIResponder, quindi non vedo motivi per cui non funzioni. – amergin

+0

'UIWebView' è un contenitore per molte sottoview. – Dmitry

9

Ordinato! Ho semplicemente utilizzare una visualizzazione di testo 1x1px e utilizzare il metodo delegato textViewDidChangeSelection:

EDIT: Per iOS 6 ho dovuto modificare la visualizzazione del testo a 50x50px (o almeno abbastanza per visualizzare in realtà il testo) per far funzionare tutto

Sono anche riuscito a sopprimere la tastiera su schermo quando il pedale è disconnesso.

Questo è il mio codice in 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 è dichiarata come bool nel mio file di intestazione.

Quindi aggiungere questi metodi:

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

Spero che questo codice aiuta a qualcun altro che sta cercando una soluzione a questo problema. Sentiti libero di usarlo.

+1

lavorato molto per me, ma ho dovuto mettere il codice di inizializzazione in viewDidAppear non viewDidLoad. – Bryan

+2

Penso che questo porti ad un ciclo infinito, perché l'impostazione della proprietà 'selectedRange' (in' textViewDidChangeSelection') attiva un altro 'textViewDidChangeSelection', sebbene questa idea mi abbia davvero aiutato. –

+0

Non causerà un loop infinito, dato che "selectedRange" è impostato solo se la posizione del cursore è 0 o 2. – colincameron

Problemi correlati