1

I need to draw views after time interval for example creating the first view then wait 5 second and create the other one, I am using this:

    -(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
    Look at NSTimer. I believe it is just what you need. It can either one-shot or repetitive. If I understood you just right the last mode is what you need – Sergii Martynenko Jr Jun 23 '15 at 08:36
  • I am using the performSelector method is the NSTimer a better solution ? – Abdelrahman Manna Jun 23 '15 at 08:40
  • @AbdelrahmanManna Not really, I have found that NSTimer sometimes is tricky/buggy. Take a look at [this question](http://stackoverflow.com/questions/8119531/scheduledtimerwithtimeinterval-vs-performselector-with-delay-with-ios-5-0) to see the difference – Daniel Jun 23 '15 at 08:43
  • Well, can't see anything wrong with your approach except you can't stop it. Sp, once this method starts running, it will be till closing application – Sergii Martynenko Jr Jun 23 '15 at 08:44
  • @SergiiMartynenkoJr His code will call drawView just once – Daniel Jun 23 '15 at 09:14

1 Answers1

-1

Create this method:

-(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];
}

and then just call [self drawViewsEvery5Seconds];

UPDATE

To stop the drawing of the views call [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(drawViewsEvery5Seconds) object:nil];

Daniel
  • 20,420
  • 10
  • 92
  • 149
  • you mean I have to do this ? -(void) drawView { int x=16; UIView *view = [[UIView alloc] initWithFrame:CGRectMake(x, 580, 16, 25)]; view.backgroundColor = [UIColor blackColor]; [self.view addSubview:view]; } -(void) drawViewsEvery5Seconds { 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:5]; } – Abdelrahman Manna Jun 23 '15 at 08:46
  • No, just use `drawViewsEvery5Seconds`. I have corrected it, I forgot drawView at the selector. Also made the code a bit clearer – Daniel Jun 23 '15 at 08:49
  • (I suppose you want to create a new view to the right every 5 seconds) – Daniel Jun 23 '15 at 08:50
  • so what is "lastview" ? – Abdelrahman Manna Jun 23 '15 at 08:53
  • It is the last created view, you just need to set `UIView *lastView;` in your .h – Daniel Jun 23 '15 at 09:11
  • One last thing, I want to make this code for just one view and expand its width , the thing I am trying to do is "Bar loading progress" – Abdelrahman Manna Jun 23 '15 at 09:25