2010-07-19 77 views
15

我還沒有找到一個很簡單的方法來做到這一點。我見過的方式需要所有這些定時器和東西。有沒有什麼簡單的方法可以容納一個UIButton,並讓它一遍又一遍地重複這個動作,直到它被釋放爲止?UIButton觸摸並保持

回答

17

您可以執行以下操作:製作一個NSTimer,在應用程序啓動時或在viewDidLoad中啓動,並且還會生成布爾值。

例如:

//Declare the timer, boolean and the needed IBActions in interface. 
@interface className { 
NSTimer * timer; 
bool g; 
} 
-(IBAction)theTouchDown(id)sender; 
-(IBAction)theTouchUpInside(id)sender; 
-(IBAction)theTouchUpOutside(id)sender; 

//Give the timer properties. 
@property (nonatomic, retain) NSTimer * timer; 
在實現文件

現在(.M):

//Synthesize the timer 
@synthesize timer; 
//When your view loads initialize the timer and boolean. 
-(void)viewDidLoad { 
    g = false; 
    timer = [NSTimer scheduledTimerWithInterval: 1.0 target:self selector:@selector(targetMethod:) userInfo:nil repeats: YES]; 
} 

現在做一個IBAction爲爲 「觸落」 設置布爾值讓說真的。然後爲「Touch Up Inside」和「Touch Up Outside」創建另一個IBAction按鈕,將布爾值指定爲false。

例如:

-(IBAction)theTouchDown { 
    g = true; 
} 

-(IBAction)theTouchUpInside { 
    g = false; 
} 

-(IBAction)theTouchUpOutside { 
    g = false; 
} 

然後在的NSTimer方法,把以下內容:(假設g是您已聲明布爾)我希望這一切都簡化

-(void) targetmethod:(id)sender { 
    if (g == true) { 
     //This is for "Touch and Hold" 
    } 
    else { 
     //This is for the person is off the button. 
    } 
} 

...我知道它仍然使用計時器,但沒有別的辦法。

+1

很 「哈克」,但它完美! – 2010-07-20 01:26:57

+0

刪除時間及其完美 – junaidsidhu 2014-05-21 10:40:02

11

不幸的是,它仍然看起來像你必須爲自己編寫這個功能。最簡單的方法(你還需要一個計時器雖然):

執行要重複動作的函數:

-(void) actionToRepeat:(NSTimer *)timer 
{ 
    NSLog(@"Action triggered"); 
} 

在您的.h文件中聲明,並設置屬性的計時器:

@interface ClassFoo 
{ 
    NSTimer* holdTimer; 
} 

然後在.M做兩個IBActions:

-(IBAction) startAction: (id)sender 
{ 
    holdTimer = [NSTimer scheduledTimerWithTimeInterval:0.4 target:self selector:@selector(actionToRepeat:) userInfo:nil repeats:YES]; 
    [holdTimer retain]; 
} 

-(IBAction) stopAction: (id)sender 
{ 
    [holdTimer invalidate]; 
    [holdTimer release]; 
    holdTimer = nil; 
} 

然後就鏈接到Touch Down IB中的事件從按鈕到startActionTouch Up Inside到「停止操作」。這不是一個班輪,但它允許您自定義動作重複的速率,並允許您從另一個插座/動作觸發它。

如果您打算經常使用此功能,那麼您可以考慮子類化UIButton並添加此功能 - 然後實施第一次只會(稍微)痛苦。

+0

我認爲invalidate永久刪除計時器,不是嗎?從iPhone SDK參考:invalidate: 停止接收器再次觸發並請求將其從運行循環中移除。 – 2010-07-19 20:51:13

+0

是的 - 這不是必需的功能嗎?然後在下一次按下按鈕 – davbryn 2010-07-20 06:38:02

1

我不能答覆的第一個,但此行:

timer = [NSTimer scheduledTimerWithInterval: 1.0 target:self selector:@selector(targetMethod:) userInfo:nil repeats: YES]; 

至少的iOS 4.1和更高版本的需求是:

timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:@selector(targetMethod:) userInfo:nil repeats: YES]; 
9

使用this NBTouchAndHoldButton的其他方式。這正是你想要的,並且很容易實現它:

TouchAndHoldButton * pageDownButton = [TouchAndHoldButton buttonWithType:UIButtonTypeCustom]; 
[pageDownButton addTarget:self action:@selector(pageDownAction:) forTouchAndHoldControlEventWithTimeInterval:0.2]; 

祝你好運!

+1

時創建一個新的計時器如何將任何人導入到.swift控制器,只要我們將標題導入到橋接頭? – 2014-10-22 14:59:53

+2

@GeorgeAsda Swift版本:https://github.com/vvit/TouchAndHoldButton – SoftDesigner 2015-05-21 18:37:42

+1

體驗開放源代碼世界演變的開心時刻...... – ingaham 2015-05-21 20:24:00

0

我知道這是一個老問題,但作爲一個簡單的方法,像考慮使用「[NSObject performSelector:withObject:afterDelay:]」來重複調用任何特定時間間隔的方法。

在這種情況下:

NSTimeInterval someTimeInterval = 1; 

- (IBAction)action:(id)sender { 
    UIButton * const button = sender; 
    if (button.state != UIControlStateHighlighted) { 
     return; 
    } 
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:_cmd object:sender]; 
    [self performSelector:_cmd withObject:sender afterDelay:someTimeInterval]; 
}