2011-11-02 11 views
11

मुझे पता है कि इससे पहले पूछा गया है, और मैंने जो जवाब देखा है, वे हैं "बाहरी कीबोर्ड की आवश्यकता नहीं है, क्योंकि यह यूआई दिशानिर्देशों के खिलाफ है"। हालांकि, मैं इस तरह के पैर पेडल का उपयोग करना चाहता हूं: http://www.bilila.com/page_turner_for_ipad मेरे ऐप के पृष्ठों (स्वाइप करने के अलावा) के बीच बदलने के लिए। यह पृष्ठ टर्नर एक कीबोर्ड को अनुकरण करता है और ऊपर/नीचे तीर कुंजियों का उपयोग करता है।मैं बाहरी कीबोर्ड तीर कुंजी का जवाब कैसे दे सकता हूं?

तो मेरा प्रश्न यह है कि मैं इन तीर कुंजी घटनाओं का जवाब कैसे दूं? अन्य ऐप्स प्रबंधित होने पर यह संभव होना चाहिए, लेकिन मैं एक खाली चित्रण कर रहा हूं।

उत्तर

21

जो लोग आईओएस 7 के तहत समाधान की तलाश में हैं, उनके लिए KeyCommands नामक एक नई यूआईआरस्पेन्डर संपत्ति है। 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

सॉर्ट किया गया! मैं बस एक 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

मेरे लिए महान काम किया है, लेकिन मैं viewDidLoad नहीं viewDidAppear में प्रवर्तन कोड डाल करने के लिए किया था। – Bryan

+2

मुझे लगता है कि यह एक अनंत लूप की ओर जाता है, क्योंकि 'चयनित श्रेणी' प्रॉपर्टी सेट करना ('textViewDidChangeSelection' में) एक और 'textViewDidChangeSelection' ट्रिगर करता है, हालांकि इस विचार ने वास्तव में मेरी मदद की। –

+0

यह एक अनंत लूप का कारण नहीं बनता है, क्योंकि 'चयनित रेंज' केवल तभी सेट किया जाता है जब कर्सर स्थान 0 या 2. – colincameron

संबंधित मुद्दे