UIButton的两种动作方法;下一步追踪:

时间:2009-12-18 06:37:41

标签: iphone avaudioplayer

我正在创建一个音乐播放器应用程序,它需要从一个按钮调用2个动作,一个通过触摸内部事件跳到下一个轨道,以及其他快速转发'长按'的curenttrack。我不知道哪个事件指向这个长按,我认为它是触地,但它只在按住按钮时起作用。当我释放按钮时,轨道被跳到下一个项目。请帮忙

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

2 个答案:

答案 0 :(得分: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之前的评论中注明的内容。我没有测试过这个,但它应该可以工作。

答案 1 :(得分:0)

你可以将你的按钮的类子类化,并围绕UIResponder的方法玩一下。例如,在touchesBegan方法中,您可以触发一些计时器和调用方法,该方法将移动抛出您的文件并使toucesEnded方法中的此计时器无效