2017-01-18 4 views
2

키 명령이 등록 된 경우 사용자가 키를 너무 오래 누르고 있으면 여러 번 호출 될 수 있습니다. 이렇게하면 ⌘N이 반복적으로 새보기를 여러 번 열 수있는 것과 같이 매우 이상한 효과가 발생할 수 있습니다. 부울 "이미 트리거 된"플래그와 같은 것을 사용하지 않고이 동작을 중지하는 쉬운 방법이 있습니까? 당신이 새로운보기 때문에,이 처리 할 필요가 없습니다 일반적으로 TestKeyCommands.zipUIKeyCommand 반복 작업 중지

답변

2

: 샘플 프로젝트는 여기에서 다운로드 할 수 있습니다

#pragma mark - KeyCommands 

- (BOOL)canBecomeFirstResponder { 
    return YES; 
} 

- (NSArray<UIKeyCommand *>*)keyCommands { 
    return @[ 
      [UIKeyCommand keyCommandWithInput:@"O" modifierFlags:UIKeyModifierCommand action:@selector(keyboardShowOtherView:) discoverabilityTitle:@"Show Other View"], 
      [UIKeyCommand keyCommandWithInput:@"S" modifierFlags:UIKeyModifierCommand action:@selector(keyboardPlaySound:) discoverabilityTitle:@"Play Sound"], 
      ]; 
} 

- (void)keyboardShowOtherView:(UIKeyCommand *)sender { 
    NSLog(@"keyboardShowOtherView"); 
    [self performSegueWithIdentifier:@"showOtherView" sender:nil]; 
} 

- (void)keyboardPlaySound:(UIKeyCommand *)sender { 
    NSLog(@"keyboardPlaySound"); 
    [self playSound:sender]; 
} 

#pragma mark - Actions 

- (IBAction)playSound:(id)sender { 
    AudioServicesPlaySystemSound(1006); // Not allowed in the AppStore 
} 

: 여기

내가 두 개의 서로 다른 키 명령을 등록 방법 보통 첫 번째 응답자가되어 반복을 멈 춥니 다. playSound의 경우, 사용자는 무슨 일이 일어나고 있는지 깨닫고 열쇠에서 손가락을 떼어냅니다.

즉, 특정 키를 절대로 반복해서는 안되는 실제 사례가 있습니다. 애플이 공개 API를 제공하면 좋을 것이다. 내가 말할 수있는 한, 그들은하지 않습니다.

코드에서 'AppStore에서 허용되지 않는'주석이 주어지면 비공개 API를 사용하면 괜찮은 것처럼 보입니다. 이 경우 keyCommand에 대한 반복을 사용 중지 할 수 있습니다.

UIKeyCommand *keyCommand = [UIKeyCommand ...]; 
[keyCommand setValue:@(NO) forKey:@"_repeatable"]; 
+0

이것이 공개 API의 일부가 아니라는 점에 놀랐습니다. 도와 주셔서 감사합니다! – Brent