Skip to content
This repository has been archived by the owner on Nov 10, 2023. It is now read-only.

Is there a way to detect long press of volume button? #27

Open
hyd00 opened this issue Aug 5, 2016 · 21 comments
Open

Is there a way to detect long press of volume button? #27

hyd00 opened this issue Aug 5, 2016 · 21 comments

Comments

@hyd00
Copy link

hyd00 commented Aug 5, 2016

No description provided.

@jpsim
Copy link
Owner

jpsim commented Aug 5, 2016

I haven't used this library myself for a few years now, so I'm not sure how you could go about doing this. I hope someone else who's used it more actively recently can chime in. Maybe @joestelmach?

@joestelmach
Copy link
Contributor

joestelmach commented Aug 5, 2016

I think you'll continue to get notifications when you hold the button down, so you can do something like this:

-(void)onVolumeUp: {
  if(self.timer) {
    [self.timer invalidate];
  }
  self.timer = [NSTimer scheduledTimerWithTimeInterval:0.5
                                               target:self
                                             selector:@selector(onTimerFire)
                                             userInfo:nil
                                              repeats:NO];
}

-(void)onTimerFire {
  NSLog(@"Long Press");
  self.timer = nil;
}

You'll need some way of counting as well, since the timer will fire even if there's only one invocation of onVolumeUp

Keep in mind that the onVolumeUp method will need to run on the same thread every time for the invalidate call to work (https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSTimer_Class/)

@hyd00
Copy link
Author

hyd00 commented Aug 6, 2016

Thanks man! This works!

@hyd00 hyd00 closed this as completed Aug 6, 2016
@hyd00
Copy link
Author

hyd00 commented Aug 6, 2016

@joestelmach Please excuse me for asking, Your solution works perfectly, and you are right, it also fires on short press of the button. Any hint as to how I can implement counting to differentiate between long and short press?

Thanks!

@hyd00 hyd00 reopened this Aug 6, 2016
@hyd00
Copy link
Author

hyd00 commented Aug 8, 2016

I did this but it's not working properly:

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

-(void)onVolumeUp {
    if(self.volButtonTimer) {
        [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;

}

The DO SOMETHING block fires only when I release the button after long press. What happening is, I believe, that OnTimeFire gets called very fast, even before the secondElapsed increments to 2.

Any guides how can I fix this? @joestelmach

Thanks in anticipation :)

@joestelmach
Copy link
Contributor

you just need to incremement a count within the if(self.volButtonTimer), then check that the count is more than one within onTimerFire

@hyd00
Copy link
Author

hyd00 commented Aug 9, 2016

Tried. Same issue. When release the button after long press, it fires the desired method. :(

@hyd00
Copy link
Author

hyd00 commented Aug 31, 2016

Still waiting for any more help :(

@joestelmach
Copy link
Contributor

paste your code and let's look at it

@hyd00
Copy link
Author

hyd00 commented Aug 31, 2016

//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 > 0)
    {
        //Do Something
    }
    else secondsElapsed = 0;

}

@joestelmach
Copy link
Contributor

How will secondsElapsed ever be greater than 1?

@hyd00
Copy link
Author

hyd00 commented Aug 31, 2016

Sorry.Edited.Please,check

@timov7
Copy link

timov7 commented Oct 11, 2016

Hi, maybe this is a bit late, but maybe someone might find useful. If you would like to have methods called when there is long press detected you might verify secondsElapsed in onVolumeUp method like this:
// let's say when count is 3 is enough to admit that it was long press
`-(void)onVolumeUp {

if(self.volButtonTimer) {
    if(secondsElapsed == 3) {
         secondsElapsed = 0; 
         // call your long press method here
    }
    secondsElapsed ++;
    [self.volButtonTimer invalidate];
}
self.volButtonTimer = [NSTimer scheduledTimerWithTimeInterval:0.5
                                              target:self
                                            selector:@selector(onTimerFire)
                                            userInfo:nil
                                             repeats:NO];

}`

@hyd00
Copy link
Author

hyd00 commented Oct 11, 2016

@timov7 let me try and get back. Did you try it urself though?

@timov7
Copy link

timov7 commented Oct 11, 2016

Yes, it is working for me.

@hyd00
Copy link
Author

hyd00 commented Oct 11, 2016

It looks good enough to detect long press yes. But does it also help u detect short press?

@timov7
Copy link

timov7 commented Oct 11, 2016

You can detect short press in onTimeFire method
`-(void)onTimeFire {

self.volButtonTimer = nil;

if (self.secondsElapsed != 0) {
    self.secondsElapsed = 0;

} else {
    NSLog(@"short");
    // short press method
}

}`

@hyd00
Copy link
Author

hyd00 commented Oct 15, 2016

Hey @timov7 .. your solution does work, however it is never going in the following block

else {

NSLog(@"short");

// short press method
}

@timov7
Copy link

timov7 commented Oct 15, 2016

Yes, you need to make timer nil each time onTimeFire gets called. Otherwise secondsElapsed will increase continuously in onVolumeUp method.

@hyd00
Copy link
Author

hyd00 commented Oct 15, 2016

Still confused lol ... But thanks a lot for pitching in :)

P.S You're Beautiful - James Blunt

@timov7
Copy link

timov7 commented Oct 15, 2016

I am happy to help. :) See my edited answer for onTimeFire method.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants