我需要在时间间隔之后绘制视图,例如创建第一个视图然后等待5秒并创建另一个视图,我正在使用它:
-(void) drawView
{
int x=0;
x+=16;
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(x, 580, 16, 25)];
view.backgroundColor = [UIColor blackColor];
[self.view addSubview:view];
}
[self performSelector:@selector(drawView) withObject:nil afterDelay:4];
答案 0 :(得分:-1)
创建此方法:
-(void) drawViewsEvery5Seconds
{
if(!lastView)//should be a member variable
{
lastView = [[UIView alloc] initWithFrame:CGRectMake(0, 580, 16, 25)];
}
else
{
lastView = [[UIView alloc] initWithFrame:CGRectMake(lastView.frame.origin.x+16, 580, 16, 25)];
}
lastView.backgroundColor = [UIColor blackColor];
[self.view addSubview:lastView];
[self performSelector:@selector(drawViewsEvery5Seconds) withObject:nil afterDelay:5];
}
然后只需致电[self drawViewsEvery5Seconds];
<强>更新强>
要停止绘制视图,请致电[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(drawViewsEvery5Seconds) object:nil];