简体   繁体   中英

Detect long and short press of volume button - iOS

I have been looking around like crazies. I need to do custom actions on volume button long and short press (like snapchat, short press take picture, long press record video) but have failed.

I have used https://github.com/jpsim/JPSVolumeButtonHandler which has allowed me to detect the volume button press event. but after trying very hard I have failed to detect when its a long press and when its a short press.

also refer to Detect iPhone Volume Button Hold? (iOS 8)

my code

//Volume button handler

self.volumeButtonHandler = [JPSVolumeButtonHandler volumeButtonHandlerWithUpBlock:^{
        // Volume Up Button Pressed
        [self onVolumeUp];
    } downBlock:^{
        // Volume Down Button Pressed
    }];

-(void)onVolumeUp {
    if(self.volButtonTimer) {
        secondsElapsed = 1; //To detect long press
        [self.volButtonTimer invalidate];
    }
    self.volButtonTimer = [NSTimer scheduledTimerWithTimeInterval:0.5
                                                  target:self
                                                selector:@selector(onTimerFire)
                                                userInfo:nil
                                                 repeats:NO];

}

-(void)onTimerFire {
    NSLog(@"Long Press %d",secondsElapsed);
    if (secondsElapsed > 1)
    {
        //Do Something
    }
    else secondsElapsed = 0;

}

Please I need help on this. Searched all over google but failed. If snapchat is doing it, means its doable.

Thank you

I am facing the same issue right now. What I was able to find out is that Apple will reject your app if it alters some basic hardware functionalities. See this link: https://developer.apple.com/app-store/review/guidelines/ . On the other hand it is misleading since there are many camera apps on App Store, like Snapchat, that clearly use volume buttons / long press for other purposes.

Anyways, I managed to handle long press, see this thread: https://github.com/jpsim/JPSVolumeButtonHandler/issues/27#issuecomment-253979943

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM