同时按下按钮

时间:2014-07-25 20:38:13

标签: ios objective-c

我正在尝试构建一个需要同时按下两个按钮的应用程序。

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

使用“Touch Down”手势选项将两个按钮连接到View Controller。当我同时按下两个按钮(按一下)时,控制台窗口会打印:

One button pressed
Two buttons pressed

这会干扰我的应用程序的工作方式。我只想要控制台打印

Two buttons pressed

由于

2 个答案:

答案 0 :(得分:1)

我理解的是,当按下两个按钮时,您需要采取一些措施。你尝试的Watever,这些按钮的触摸之间会有一个延迟。更好的方法是检查两个按钮上的按下是否完成。希望以下适合您 -

    @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");
        }
    }

答案 1 :(得分: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;
}

或者,您也可以在触摸停止时启动计时器,并检查在指定的时间间隔内是否发生第二次触摸。