detecting long tap on iPhone

前端 未结 3 2165
后悔当初
后悔当初 2021-02-06 07:17

I am working on an iPhone app which requires me to check if the button has been tapped & held pressed for 6 seconds & then fire an action which is playing some sort of s

3条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-06 08:06

    Here is my solution.

    - (IBAction) micButtonTouchedDownAction {
        self.micButtonTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(micButtonAction:) userInfo:nil repeats:YES];
        self.micButtonReleased = FALSE;
    }
    
    - (IBAction) micButtonTouchedUpInsideAction {
        self.micButtonReleased = TRUE;
    }
    
    - (IBAction) micButtonTouchedUpOutsideAction {
        self.micButtonReleased = TRUE;
    }
    
    - (void) micButtonAction:(NSTimer *)timer {
        [self.micButtonTimer invalidate];
        self.micButtonTimer = nil;
    
        if(self.micButtonReleased) {
            NSLog(@"Tapped");
        }
        else {
            NSLog(@"Touched");
        }
    }
    

提交回复
热议问题