2014-07-25 2 views
0

두 개의 단추를 동시에 눌러야하는 응용 프로그램을 작성하려고합니다.동시 단추 누르기

if (self->button1.touchInside && self->button2.touchInside) { 
    NSLog(@"Two buttons pressed"); 
    } 
else if (!self->button1.touchInside | !self->button2.touchInside){ 
    NSLog(@"One button pressed"); 
    } 

두 버튼 모두 '터치 다운'제스처 옵션을 사용하여 View Controller에 연결됩니다. 동시에 (두 번 누름으로) 두 버튼을 동시에 누르면 콘솔 창에 다음과 같이 인쇄됩니다.

One button pressed 
Two buttons pressed 

이는 응용 프로그램의 작동 방식을 방해합니다. 난 단지 콘솔이

Two buttons pressed 

감사

을 인쇄 할
+2

글쎄, 항상 첫 번째 버튼에서 두 번째 버튼까지 지연이있을 것입니다. 나만의 타이머 확인을 추가하고 N 밀리 초 내에 두 개의 프레스를 "동시"로 처리하거나 첫 번째 "터치 업"을 기다렸다가 두 개의 버튼이 눌려 있는지 확인할 수 있습니다. –

+1

그 방법을 설명해 주시겠습니까? – Kyle

+0

중복 된 질문 일 수 있습니다. 다음 링크를 확인하십시오. http://stackoverflow.com/questions/24964104/simultaneous-button-press – jsedano

답변

1

내가 알고있는 것은 두 버튼을 모두 눌러야 할 때 어떤 조치를 취해야한다는 것입니다. 당신이 시도해도,이 버튼들의 접촉 사이에 지연이있을 것입니다. 더 나은 방법은 두 버튼을 모두 눌러야하는지 확인하는 것입니다. 희망은 당신을 위해 일한다 -

@property(nonatomic, assign) BOOL firstButtonPressed; 

    @property(nonatomic, assign) BOOL secondButtonPressed; 

    //in init or viewDidLoad or any view delegates 
     _firstButtonPressed = NO; 
     _secondButtonPressed = NO; 

    //Connect following IBActions to Touch Down events of both buttons 
    - (IBAction)firstButtonPressed:(UIButton *)sender { 
     _firstButtonPressed = YES; 
      [self checkForButtonPress]; 
    } 

    - (IBAction)secondButtonPressed:(UIButton *)sender { 
     _ secondButtonPressed = YES; 
     [self checkForButtonPress];  
    } 

    - (void)checkForButtonPress { 
     if (_firstButtonPressed && _secondButtonPressed) { 
      NSlog(@"Two buttons pressed"); 
     } 
    } 
0
이 같은 두 개의 부울 플래그를 사용하여 작업을 수행 할 수 있습니다

: 터치가 다운되면 다른 방법으로, 당신은 또한 타이머를 시작할 수 있습니다

@property(nonatomic, assign) BOOL firstButtonPressed; 
@property(nonatomic, assign) BOOL secondButtonPressed; 

- (void)firstButtonTouchDown:(id)sender 
{ 
    _firstButtonPressed = YES; 

    if (_secondButtonPressed) 
    { 
     // Both buttons pressed. 
    } 
} 


- (void)secondButtonTouchDown:(id)sender 
{ 
    _secondButtonPressed = YES; 

    if (_firstButtonPressed) 
    { 
     // Both buttons pressed. 
    } 
} 


- (void)firstButtonTouchCancelled:(id)sender 
{ 
    _firstButtonPressed = NO; 
} 


- (void)secondButtonTouchCancelled:(id)sender 
{ 
    _secondButtonPressed = NO; 
} 

을 지정한 시간 간격 동안 두 번째 터치가 발생하는지 확인하십시오.

+0

이것에 아무런 문제가 없으므로 이전에 노골적인 투표를하지 않아도됩니다. +1 –

관련 문제