+ scheduledTimerWithTimeInterval: invocation: repeats:
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation *)invocation repeats:(BOOL)yesOrNo;
+ scheduledTimerWithTimeInterval: target: selector: userInfo: repeats:
+
(NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti
target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo
repeats:(BOOL)yesOrNo;
創建返回壹個新的NSTimer對象和時間表,在當前的默認模式下循環調用壹個實例方法。
+ timerWithTimeInterval: invocation: repeats:
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation *)invocation repeats:(BOOL)yesOrNo;
+ timerWithTimeInterval: target:selector: userInfo:repeats:
+
(NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget
selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;
– initWithFireDate: interval: target: selector: userInfo: repeats:
-
(id)initWithFireDate:(NSDate *)date interval:(NSTimeInterval)ti
target:(id)t selector:(SEL)s userInfo:(id)ui repeats:(BOOL)rep;
scheduledTimerWithTimeInterval:(NSTimeInterval)seconds
預訂壹個Timer,設置壹個時間間隔。
表示輸入壹個時間間隔對象,以秒為單位,壹個>0的浮點類型的值,如果該值<0,系統會默認為0.1
target:(id)aTarget
表示發送的對象,如self
selector:(SEL)aSelector
方法選擇器,在時間間隔內,選擇調用壹個實例方法
userInfo:(id)userInfo
此參數可以為nil,當定時器失效時,由妳指定的對象保留和釋放該定時器。
repeats:(BOOL)yesOrNo
當YES時,定時器會不斷循環直至失效或被釋放,當NO時,定時器會循環發送壹次就失效。
invocation:(NSInvocation *)invocation
啟動 Timer
– fire
停止 Timer
– invalidate
Timer設置
– isValid
– fireDate
– setFireDate:
– timeInterval
– userInfo
NSTimeInterval類:是壹個浮點數字,用來定義秒
例子:
iphone為我們提供了壹個很強大得時間定時器 NSTimer
他可以完成任何定時功能:
我們使用起來也很簡單,只要記住三要素就可以,具體得三要素是:時間間隔NSTimeInterval浮點型,事件代理
delegate和事件處理方法@selector();就可以用
+
(NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti
target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo
repeats:(BOOL)yesOrNo; 來初始化壹個 時間定時器
下面我寫了壹個很簡單得例子
初始化壹個定時器:
-(void)initTimer
{
//時間間隔
NSTimeInterval timeInterval =1.0 ;
//定時器
NSTimer showTimer = [NSTimer scheduledTimerWithTimeInterval:maxShowTime
target:self
selector:@selector(handleMaxShowTimer:)
userInfo:nil
repeats:NO];
}
//觸發事件
-(void)handleMaxShowTimer:(NSTimer *)theTimer
{
NSDateFormatter dateFormator = [[NSDateFormatter alloc] init];
dateFormator.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSString *date = [dateformater stringFromDate:[NSDate date]];
if([date isEqualToString:@"2011-11-09 23:59:59"])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:TITLE_NAME
message:@"現在馬上就有新的壹天了!"
delegate:self
ancelButtonTitle:nil
otherButtonTitles:CONFIRM_TITLE, nil];
[alert show];
[alert release];
}
[data release];
[dateFormator release];
}
另外附壹個例子:方框賽跑
-(void)viewDidLoad
{
[super viewDidLoad];
CGRect workingFrame;
workingFrame.origin.x = 15;
workingFrame.origin.y = 400;
workingFrame.size.width = 40;
workingFrame.size.height = 40;
for(int i = 0; i < 6; i++)
{
UIView *myView = [[UIView alloc] initWithFrame:workingFrame];
[myView setTag:i];//標記方塊
[myView setBackgroundColor:[UIColor blueColor]];
workingFrame.origin.x = workingFrame.origin.x + workingFrame.size.width + 10;
[self.view addSubview:myView];
}
}