1

我正在创建一个音乐播放器应用程序,它需要从一个按钮调用 2 个操作,一个是通过触摸内部事件跳到下一首曲目,另一个是在“长按”的情况下快进当前曲目。我不知道哪个事件指向这个长按,我认为它是触地,但它只在按住按钮时起作用。当我松开按钮时,曲目被跳到下一个项目。请帮忙

AVAudioPlayer *appSoundPlayer;// declared in .h file

在m文件中,方法:

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

        appSoundPlayer.currentTime = timex;
        timex = 0;
}
4

2 回答 2

4

就我个人而言,我只是在您的视图控制器或按钮子类中使用整数来跟踪按钮的状态。如果您跟踪按钮的操作,您可以控制每个操作的操作。在您的 .h 文件中放入如下内容:

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

然后在你的 .m 文件中加入一些这样的东西:

@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;

}

之后,只需将按钮的操作链接到我在 IBactions 之前的评论中提到的内容。我没有对此进行测试,但它应该可以工作。

于 2009-12-18T07:05:44.070 回答
0

您可以子类化您的按钮类,并围绕 UIResponder 的方法进行一些操作。例如,在touchesBegan方法中,您可以触发一些计时器并调用方法,这将移动您的文件并使该toucesEnded方法中的计时器无效

于 2009-12-18T07:08:34.730 回答