views:

329

answers:

2

I am creating a music player application and it need that 2 actions to be called from a single button, one to skip to next track by touch up inside event and other to fast forward the curenttrack incase of a 'long press'. I don't know which event is pointing to this long press, i thought it to be touch down, but it worked only while holding the button. When i released the button, the track was skipped to next item. pls help

AVAudioPlayer *appSoundPlayer;// declared in .h file

In m file, the method:

-(void)seekForwards{
NSTimeInterval timex;
timex = appSoundPlayer.currentTime;
     timex = timex+5; // forward 5 secs

     appSoundPlayer.currentTime = timex;
     timex = 0;
}
+3  A: 

Personally I'd just track the button's state with an integer on your view controller or within a button subclass. If you track what the button is doing you can control what each of the actions do. In your .h file put in some stuff like this:

enum {
 MyButtonScanning,
 MyButtonStalling,
 MyButtonIdle
};



@interface YourClass : UIViewController {
 NSInteger buttonModeAt;
}
@property (nonatomic) NSInteger buttonModeAt;
-(IBAction)buttonPushedDown:(id)sender;
-(void)tryScanForward:(id)sender;
-(IBAction)buttonReleasedOutside:(id)sender;
-(IBAction)buttonReleasedInside:(id)sender;
@end

And then in your .m file throw in some of this stuff:

@implementation YourClass
///in your .m file
@synthesize buttonModeAt;


///link this to your button's touch down
-(IBAction)buttonPushedDown:(id)sender {
 buttonModeAt = MyButtonStalling;
 [self performSelector:@selector(tryScanForward:) withObject:nil afterDelay:1.0];
}

-(void)tryScanForward:(id)sender {
 if (buttonModeAt == MyButtonStalling) {
  ///the button was not released so let's start scanning
  buttonModeAt = MyButtonScanning;

  ////your actual scanning code or a call to it can go here
  [self startScanForward];
 }
}

////you will link this to the button's touch up outside
-(IBAction)buttonReleasedOutside:(id)sender {
 if (buttonModeAt == MyButtonScanning) {
  ///they released the button and stopped scanning forward
  [self stopScanForward];
 } else if (buttonModeAt == MyButtonStalling) {
  ///they released the button before the delay period finished
  ///but it was outside, so we do nothing
 }

 self.buttonModeAt = MyButtonIdle;
}

////you will link this to the button's touch up inside
-(IBAction)buttonReleasedInside:(id)sender {
 if (buttonModeAt == MyButtonScanning) {
  ///they released the button and stopped scanning forward
  [self stopScanForward];
 } else if (buttonModeAt == MyButtonStalling) {
  ///they released the button before the delay period finished so we skip forward
  [self skipForward];
 }

 self.buttonModeAt = MyButtonIdle;

}

After that just link the button's actions to what I've noted in the comments before the IBactions. I haven't tested this but it should work.

mjdth
just put this into a class and made a quick button for it and everything seems to work fine. All you have to do is add your startScanForward, stopScanForward, and skipForward methods.
mjdth
thanks for the reply. but no changes yet, may be the problem with ma startScanForward method, i dont know what to do to stop stopping. i'm adding ma startScanningForward method to ma qn, i've given the name seekForwards: to it.
Nithin
I've created a simple uicontrolviewer with these functions and it works fine. This will answer your question as to how to get the button itself to do both a skip and a seek based on holding. Do you want the actual files themselves? Or are you having a problem with your scan/skip code?
mjdth
i'm having problem with ma scan skip code.
Nithin
What is it doing incorrectly? IF it's skipping to the next song after you release the button you need to be sure to fully implement all of the code i've added above.
mjdth
have u seen the method i've added to the quetion. it is the one that i use in place of 'startScanForward' method you've given. But confused with the method 'stopScanForward' method. dont know what to do inside it.:(
Nithin
thats what i meant. startScanForward and skipForward methods are done successfully. but i dont know what to do with stopScanForward method. What to do inside that???
Nithin
done it... my carelessness paid for the time...
Nithin
A: 

you can subclass your button's class and play a bit around UIResponder's methods. For example, in touchesBegan method you can fire some timer and call method which will move throw your file and invalidate this timer in the toucesEnded method

Morion