在时间间隔之后绘制视图

时间:2015-06-23 08:33:32

标签: ios objective-c

我需要在时间间隔之后绘制视图,例如创建第一个视图然后等待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];

1 个答案:

答案 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];