Iphone 之Timer

[NSTimer scheduledTimerWithTimeInterval:1.5f 
                                 target:self 
			       selector:@selector(stopActivityView) 
                               userInfo:nil 
				repeats:NO];


定义一个时间戳,在这段时间内进行一些别的处理。

第一个参数是指时间的长度。以秒为单位。

第二个参数是指Timer的上下文内容。一般是在那个View调用,就是指View对象本身。也就是选择器所要发送消息的对象。一般是SELF。

第三个参数是一个选择器。选中等待时间后所要处理的事情或方法。需要自己定义一个方法。然后作为一个参数传给选择器。

第四个参数是Timer的用户信息。一般为空。

第五个参数为一个BOOL型参数。如果为YES,Timer会不断的重复定义时间。知道Timer无效。如果为NO,则等到Timer激活后就作废。

官网的解释如下:

scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:
Creates and returns a new NSTimer object and schedules it on the current run loop in the default mode.

+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats
Parameters
seconds
The number of seconds between firings of the timer. If seconds is less than or equal to 0.0, this method chooses the nonnegative value of 0.1 milliseconds instead.
target
The object to which to send the message specified by aSelector when the timer fires. The target object is retained by the timer and released when the timer is invalidated.
aSelector
The message to send to target when the timer fires. The selector must have the following signature:
- (void)timerFireMethod:(NSTimer*)theTimer
The timer passes itself as the argument to this method.
userInfo
The user info for the timer. The object you specify is retained by the timer and released when the timer is invalidated. This parameter may be nil.
repeats
If YES, the timer will repeatedly reschedule itself until invalidated. If NO, the timer will be invalidated after it fires.

使用的一个例子。就是一个等待对话框。

等待一定时间后显示另一个界面或者是因为网络连接的等待。

- (id)initWithFrame:(CGRect)frame{
	if (self = [super initWithFrame:frame]) {
		self.backgroundColor = [UIColor blackColor];
		self.alpha = 0.8;
		[self.layer setBorderWidth:2.0];
		[self.layer setBorderColor:[UIColor clearColor].CGColor];
		[self.layer setCornerRadius:7.0];//圆角
		
		UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 45, frame.size.width, 20)];
		label.backgroundColor = [UIColor clearColor];
		label.textAlignment = UITextAlignmentCenter;
		label.textColor = [UIColor whiteColor];
		label.text = @"加载数据";
		self.activityLabel = label;
		[self addSubview:label];
		[label release];
		
		UIActivityIndicatorView *activity = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
		activity.hidesWhenStopped = YES;
		activity.frame = CGRectMake(frame.size.width/2-10, 15, 20, 20);
		[activity startAnimating];
		self.activityView = activity;
		[self addSubview:activity];
		[activity release];
	}
	return self;
}

- (void)stopActivityAnimating {
	//UIActivityIndicatorView *activityView = (UIActivityIndicatorView*)[self viewWithTag:ACTIVITYVIEW_TAG];
	[activityView stopAnimating];
	[self removeFromSuperview];
}

@end


这是我自己定义的一个View。也就是我们的等待界面。

然后就是开启和结束。这里我定义了两个方法。为了方便在其他View调用这个等待界面。

- (void)ActivityView{
	CustomActivityView *activityView = [[CustomActivityView alloc]initWithFrame:CGRectMake(105, 143, 100, 75)];
	activityView.tag = 1234;
	[self.view addSubview:activityView];
	[activityView release];
	[NSTimer scheduledTimerWithTimeInterval:1.5f 
									 target:self 
								   selector:@selector(stopActivityView) 
								   userInfo:nil 
									repeats:NO];
}
- (void)stopActivityView {
	CustomActivityView *activity_View = (CustomActivityView *)[self.view viewWithTag:1234];
	[activity_View removeFromSuperview];
		[self hideSetPasswordView];
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值