首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iPhone SDK: UIButton repeat with repeat delay

iPhone SDK: UIButton repeat with repeat delay
EN

Stack Overflow用户
提问于 2012-10-19 06:40:49
回答 1查看 989关注 0票数 1

我已经用来自另一个堆栈溢出线程( UIButton Touch and Hold )的代码在按钮(UIButton) hold上实现了一个重复的任务。

由于各种原因,我希望再次延期。definition of repeat delay

然而,我不能完全理解如何做到这一点。对于iPhone的编码,我还是个新手。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-10-19 07:02:11

灵感来自于你建议的代码,你可以这样做:

创建一个在按下按钮时启动的NSTimer,并每隔x秒触发一次方法。

报头(.h):

代码语言:javascript
复制
// Declare the timer and the needed IBActions in interface.
@interface className {
    NSTimer * timer;
}
-(IBAction)theTouchDown(id)sender;
-(IBAction)theTouchUpInside(id)sender;
-(IBAction)theTouchUpOutside(id)sender;

// Give the timer properties.
@property (nonatomic, retain) NSTimer * timer;

实现文件(.m):

代码语言:javascript
复制
// Synthesize the timer
// ...   

为"Touch Down“创建一个IBAction,以启动计时器,该计时器将每隔2秒触发一次操作方法。然后为"Touch Up Inside“和"Touch Up Outside”创建另一个IBAction,以使计时器无效。

例如:

代码语言:javascript
复制
-(IBAction)theTouchDown {

    self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0
                                                  target:self
                                                selector:@selector(action:)
                                                userInfo:nil
                                                 repeats:NO];
}

-(IBAction)theTouchUpInside {

    [self.timer invalidate];
    self.timer = nil;
}

-(IBAction)theTouchUpOutside {

    [self.timer invalidate];
    self.timer = nil;
}

然后,在由NSTimer触发的方法中,执行所需的任何操作:

代码语言:javascript
复制
-(void)action:(id)sender {

    // ...
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12964867

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档