UIButton的两种动作方法;下一步追踪: [英] Two action methods for an UIButton; next track and seek forward:
问题描述
我正在创建一个音乐播放器应用程序,它需要从一个按钮调用2个动作,一个通过触摸内部事件跳到下一个轨道,以及其他快速转发'长按'的curenttrack。我不知道哪个事件指向这个长按,我认为它是触地,但它只在按住按钮时起作用。当我释放按钮时,轨道被跳到下一个项目。请帮助
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
在m文件中,方法:
-(void)seekForwards{
NSTimeInterval timex;
timex = appSoundPlayer.currentTime;
timex = timex+5; // forward 5 secs
appSoundPlayer.currentTime = timex;
timex = 0;
}
推荐答案
就我个人而言,我只是跟踪按钮的状态,在视图控制器上或按钮子类中有一个整数。如果您跟踪按钮的功能,您可以控制每个操作的功能。在你的.h文件中输入这样的东西:
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
然后在你的.m文件中输入一些东西:
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;
}
之后只需将按钮的动作链接到我的动作在IBactions之前的评论中指出。我没有测试过这个,但它应该可以工作。
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.
这篇关于UIButton的两种动作方法;下一步追踪:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!