我還沒有找到一個很簡單的方法來做到這一點。我見過的方式需要所有這些定時器和東西。有沒有什麼簡單的方法可以容納一個UIButton,並讓它一遍又一遍地重複這個動作,直到它被釋放爲止?UIButton觸摸並保持
回答
您可以執行以下操作:製作一個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.
}
}
...我知道它仍然使用計時器,但沒有別的辦法。
不幸的是,它仍然看起來像你必須爲自己編寫這個功能。最簡單的方法(你還需要一個計時器雖然):
執行要重複動作的函數:
-(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中的事件從按鈕到startAction
和Touch Up Inside
到「停止操作」。這不是一個班輪,但它允許您自定義動作重複的速率,並允許您從另一個插座/動作觸發它。
如果您打算經常使用此功能,那麼您可以考慮子類化UIButton
並添加此功能 - 然後實施第一次只會(稍微)痛苦。
我認爲invalidate永久刪除計時器,不是嗎?從iPhone SDK參考:invalidate: 停止接收器再次觸發並請求將其從運行循環中移除。 – 2010-07-19 20:51:13
是的 - 這不是必需的功能嗎?然後在下一次按下按鈕 – davbryn 2010-07-20 06:38:02
我不能答覆的第一個,但此行:
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];
使用this NBTouchAndHoldButton的其他方式。這正是你想要的,並且很容易實現它:
TouchAndHoldButton * pageDownButton = [TouchAndHoldButton buttonWithType:UIButtonTypeCustom];
[pageDownButton addTarget:self action:@selector(pageDownAction:) forTouchAndHoldControlEventWithTimeInterval:0.2];
祝你好運!
時創建一個新的計時器如何將任何人導入到.swift控制器,只要我們將標題導入到橋接頭? – 2014-10-22 14:59:53
@GeorgeAsda Swift版本:https://github.com/vvit/TouchAndHoldButton – SoftDesigner 2015-05-21 18:37:42
體驗開放源代碼世界演變的開心時刻...... – ingaham 2015-05-21 20:24:00
我知道這是一個老問題,但作爲一個簡單的方法,像考慮使用「[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];
}
- 1. 觸摸UIButton觸摸
- 2. 科羅娜SDK觸摸階段 - 觸摸並保持
- 3. SpriteKit SKSpriteNode觸摸並保持速度
- 4. 保持UIButton在觸摸和iOS之後高亮7
- 5. 保持UIButton觸摸後選中/突出顯示
- 6. 如何保持UIButton突出顯示,直到第二次觸摸?
- 7. UIButton在觸摸後保留髮光
- 8. UIButton保留選中後觸摸
- 9. 檢測觸摸UIButton
- 10. UIButton觸摸事件
- 11. 取消觸摸它時的UIButton觸摸
- 12. UIButton - 觸摸並按住,點擊
- 13. UIButton不閃爍在觸摸
- 14. UIButton的可觸摸區域
- 15. UIButton觸摸事件重疊
- 16. IOS的UIButton觸摸取消
- 17. UIButton TouchUpInside觸摸位置
- 18. UIButton未註冊觸摸
- 19. 觸摸時淡出UIButton
- 20. UIButton移動和觸摸
- 21. UIButton不響應觸摸
- 22. 我無法觸摸的UIButton
- 23. 觸摸並拖動UIButton,但釋放手指時不觸發它
- 24. 觸摸保持事件在Corona SDK
- 25. 如何在Objective C中模擬觸摸並保持?
- 26. cocos2D CCSequence觸摸並保持一段時間
- 27. 如何實現觸摸並保持sprite套件?
- 28. 在平板電腦上切換觸摸並保持設置
- 29. 在Android中轉發觸摸事件並保持動力
- 30. Android - 旋轉佈局並保持視圖的觸摸功能
很 「哈克」,但它完美! – 2010-07-20 01:26:57
刪除時間及其完美 – junaidsidhu 2014-05-21 10:40:02