Creating Timers

2024-01-13 12:48
文章标签 creating timers

本文主要是介绍Creating Timers,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、说明:
如题,我们本篇讲的是创建一个定时器。定时器它是一个对象,在指定的时间间隔触发一个事件。定时器必须在一个运行循环中。定义一个定时器对象创建一个不定期的定时器,这个定时器不执行任何操作,但是它是可用的,可以在任何你需要启动它的时候变得可用。一个定时的计时器,它是被添加到运行循环中的。
创建定时器的方式有很多,其中一个比较方便的方法是:
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;

另外一个方法是:
+ (NSTimer )scheduledTimerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation )invocation repeats:(BOOL)yesOrNo;

eg:

#import "ViewController.h"@interface ViewController ()
@property (nonatomic,strong) NSTimer *paintingTimer;
@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];[self startPainting];}- (void)paint:(NSTimer *)paramTimer
{NSLog(@"Painting");
}- (void)startPainting
{// Here is a selector tha we can to callSEL selectorToCall = @selector(paint:);NSMethodSignature *methodSignature = [[self class] instanceMethodSignatureForSelector:selectorToCall];NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature];[invocation setTarget:self];[invocation setSelector:selectorToCall];// Start a schedule timer nowself.paintingTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 invocation:invocation repeats:YES];
}- (void)stopPainting
{if(self.paintingTimer != nil){[self.paintingTimer invalidate];NSLog(@"定时器停止...");}
}- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{[self stopPainting];
}- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];}@end

以上的方法是创建一个定时计时器。创建完成的时候就会启动。。

下面是创建一个不定期计时器,它需要你手动的把它添加到运行循环中。其初始化的方式有很多,其中:
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;

#import "ViewController.h"@interface ViewController ()
@property (nonatomic,strong) NSTimer *paintingTimer;
@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];self.paintingTimer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(paint2) userInfo:nil repeats:YES];[[NSRunLoop currentRunLoop] addTimer:self.paintingTimer forMode:NSDefaultRunLoopMode];
}- (void)paint2
{NSLog(@"Painting");
}- (void)stopPainting
{if(self.paintingTimer != nil){[self.paintingTimer invalidate];NSLog(@"定时器停止...");}
}- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{[self stopPainting];
}- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];}@end

另一种方式:
+ (NSTimer )timerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation )invocation repeats:(BOOL)yesOrNo;

#import "ViewController.h"@interface ViewController ()
@property (nonatomic,strong) NSTimer *paintingTimer;
@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];[self startPainting];
}- (void)paint:(NSTimer *)paramTimer
{NSLog(@"Painting");
}- (void)startPainting
{// Here is a selector tha we can to callSEL selectorToCall = @selector(paint:);NSMethodSignature *methodSignature = [[self class] instanceMethodSignatureForSelector:selectorToCall];NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature];[invocation setTarget:self];[invocation setSelector:selectorToCall];self.paintingTimer = [NSTimer timerWithTimeInterval:1.0 invocation:invocation repeats:YES];[[NSRunLoop currentRunLoop] addTimer:self.paintingTimer forMode:NSDefaultRunLoopMode];
}- (void)stopPainting
{if(self.paintingTimer != nil){[self.paintingTimer invalidate];NSLog(@"定时器停止...");}
}- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{[self stopPainting];
}- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];}@end

这篇关于Creating Timers的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/601505

相关文章

Creating OpenAI Gym Environment from Map Data

题意:从地图数据创建 OpenAI Gym 环境 问题背景: I am just starting out with reinforcement learning and trying to create a custom environment with OpenAI gym. However, I am stumped with trying to create an enviro

Error creating bean with name 'redisTemplate' defined in URL

最近使用Spring 整合Redis出现了这类问题: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'redisTemplate' defined in URL [file:/E:/workspace/spring-ssm/target/classes/spring/s

Error creating bean with name 'xxx.xx.xRequestMappingHandlerAdapter' Instantiation of bean failed

最近将maven 项目中的spring 版本更新到了5.1.6.RELEASE,但是项目启动时,出现了如下问题 ERROR org.springframework.web.context.ContextLoader - Context initialization failed org.springframework.beans.factory.BeanCreationException: Er

Creating custom and compound Views in Android - Tutorial(翻译)

Creating custom and compound Views in Android - Tutorial(翻译) 译前的: 之前做了三篇学习笔记,从知乎上面看到了这篇英文的推荐,总的来说可以是一篇导读,没有相关的学习,看这篇,可以作为一个学习脉络导向;有相关的学习底子,可以作为一个基础夯实、思维理清。没想到一翻译就是四个多小时…英语渣,很多词句都不太准确,幸好有之前的学习基础打底…

Documentation/timers/hpet.txt

如果想评论或更新本文的内容,请直接联系原文档的维护者。 如果你使用英文交流有困难的话,也可以向中文版维护者求助。 如果本翻译更新不及时或者翻译存在问题,请联系中文版维护者。 中文版维护者: 姚家珺AriosYao   ks666dejia@163.com 中文版翻译者: 姚家珺AriosYao   ks666dejia@163.com 中文版校译者: 姚家珺AriosYao   ks666d

Creating Your Own Document Management System With SharePoint 使用SharePoint创建你自己的文档管理系统

Creating Your Own Document Management System With SharePoint 使用SharePoint创建你自己的文档管理系统 With the R2 release of Dynamics AX 2012, a new feature was quietly snuck into the product that allows you to st

SP2010开发和VS2010专家食谱--第七章节--使用客户端对象模型(1)--Creating a list using a Managed OM

本文中,我们将学习如何使用托管对象模型创建列表。我们也将添加新栏,插入约10行数据到列表。本文中我们将创建一个使用generic list template的控制台应用程序。

SP2010开发和VS2010专家食谱--第六章节--Web Services和REST(6)--Creating a custom SharePoint WCF service

目前我们已经使用了SharePoint中提供的开箱即用Web Services。有时候SharePoint可以作为数据仓库提供业务数据到外部应用程序。

redis启动出错Creating Server TCP listening socket 127.0.0.1:6379: bind: No error

windows下安装redis第一次启动报错: [10044] 14 May 10:04:49.048 # Creating Server TCP listening socket 127.0.0.1:6379: bind: No error 解决方法:在命令行中运行 redis-cli.exe 127.0.0.1:6379>shutdown not connected>exit 然后

异常--Error creating bean with name 'org.springframework.context.annotation.internalAsyncAnnotationPro

一月 17, 2018 9:35:48 上午 org.apache.tomcat.util.digester.SetPropertiesRule beginWARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server