UIButton触摸并保持
我还没有找到一个很简单的方法来做到这一点。我见过的方式需要所有这些定时器和东西。有没有什么简单的方法可以容纳一个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];
}
很 “哈克”,但它完美! – 2010-07-20 01:26:57
删除时间及其完美 – junaidsidhu 2014-05-21 10:40:02