2009-12-18 43 views
1

我正在創建一個音樂播放器應用程序,它需要2個動作從一個按鈕被調用,一個通過觸摸事件和其他快速前進到下一個軌道以快速向前'長按'。我不知道哪個事件是指向這個長按,我認爲它是觸及下來的,但它只有在按住按鈕時才起作用。當我釋放按鈕時,軌道被跳到下一個項目。請幫助UIButton的兩種操作方法;下一個追蹤​​並尋求前進:

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

個人而言,我只希望跟蹤與您的視圖控制器或按鈕子內的整按鈕的狀態。如果你跟蹤按鈕的功能,你可以控制每個動作的作用。在把一些東西,這樣你的.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; 

} 

之後只是鏈接按鈕的行動,我已經在IB行動的評論中注意到了。我沒有測試過,但它應該可以工作。

+0

無效此計時器只是把它放到一個類,併爲它做了一個快速按鈕,似乎一切工作正常。您只需添加startScanForward,stopScanForward和skipForward方法。 – mjdth 2009-12-18 07:16:06

+0

感謝您的回覆。但沒有變化,可能是與馬startScanForward方法的問題,我不知道該怎麼做停止停止。我將ma startScanningForward方法添加到ma qn中,我給了名稱seekForwards:給它。 – Nithin 2009-12-18 09:12:49

+0

我用這些函數創建了一個簡單的uicontrolviewer,它工作正常。這將回答你的問題,如何讓按鈕本身做基於持有的跳過和尋找。你想自己的實際文件?或者你的掃描/跳過代碼有問題? – mjdth 2009-12-18 09:17:23

0

你可以繼承你的按鈕的類並在UIResponder的方法中播放一些東西。例如,在touchesBegan方法中,您可以觸發一些計時器和調用方法,它將移動扔你的文件,並在toucesEnded方法方法

相關問題