Dois métodos de ação para um UIBUItton; Próxima faixa e procure adiante:
-
20-09-2019 - |
Pergunta
Estou criando um aplicativo de reprodutor de música e ele precisa que duas ações sejam chamadas de um único botão, um para pular para a próxima faixa por tocar no evento interno e outro para avançar rapidamente a trilha curente em uma 'longa imprensa'. Não sei qual evento está apontando para essa imprensa há muito tempo, pensei que fosse tocar, mas funcionou apenas enquanto segurava o botão. Quando soltei o botão, a faixa foi ignorada para o próximo item. Pls ajuda
AVAudioPlayer *appSoundPlayer;// declared in .h file
No arquivo M, o método:
-(void)seekForwards{
NSTimeInterval timex;
timex = appSoundPlayer.currentTime;
timex = timex+5; // forward 5 secs
appSoundPlayer.currentTime = timex;
timex = 0;
}
Solução
Pessoalmente, eu apenas rastrearia o estado do botão com um número inteiro no seu controlador de exibição ou dentro de uma subclasse de botão. Se você rastrear o que o botão está fazendo, poderá controlar o que cada uma das ações faz. No seu arquivo .h, coloque algumas coisas assim:
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
E então, em seu .M, jogue algumas dessas coisas:
@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;
}
Depois disso, basta vincular as ações do botão ao que observei nos comentários antes das Ibações. Eu não testei isso, mas deve funcionar.
Outras dicas
Você pode subclasse a classe do seu botão e reproduzir um pouco os métodos do UIRSPONER. Por exemplo, em touchesBegan
Método, você pode disparar algum método de cronômetro e chamado que moverá o seu arquivo e invalidará esse timer no toucesEnded
método