[英]Simultaneous Button Press
我正在嘗試構建一個需要同時按下兩個按鈕的應用程序。
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
謝謝
我了解的是,當同時按下兩個按鈕時,您需要采取一些措施。 無論您嘗試什么,在這些按鈕的觸摸之間都會有一個滯后。 更好的方法是檢查是否同時按下兩個按鈕。 希望以下為您工作-
@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");
}
}
您可以使用兩個布爾標志來做到這一點:
@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;
}
或者,您也可以在觸摸下降時啟動計時器,並檢查第二次觸摸是否在您指定的時間間隔內發生。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.