Manish Agrawal
Manish Agrawal

Reputation: 11037

Animation on adding multiple subviews in iOS

I am adding 20 UIButton programmatically in UIVIew, what I have to do is add animation on these buttons such that first button appear at time 't' after that second will appear at time time 't+1' and so on. I have tried adding buttons after some delay but its not working all the buttons will be showed to view at a time.
If there is solution to do this please let me know.

for(int i = 0; i<20;i++)
{
    UIButton *button = [UIButton buttonWithType: UIButtonTypeCustom]; 
    [button setBackgroundImage:[UIImage imageNamed:@"i_setting30.png"] forState:UIControlStateNormal];  
    [button setImage:[UIImage imageNamed:@"threadmenu.png"] forState:UIControlStateNormal];
    [button addTarget: self action:@selector(threadmenu) forControlEvents:UIControlEventTouchUpInside]; 
    [self.view performSelector:@selector(addSubview:) withObject:button afterDelay:1];
    button.frame = CGRectMake(0+i*20, 0, 20, 20);
}

Upvotes: 0

Views: 277

Answers (1)

Cre-Sol InfoSystems
Cre-Sol InfoSystems

Reputation: 71

You can use NSTimer to implement this.

You can schedule a timer that will call the desired method after every 't' time until all 20 buttons has been added on the view.

NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval:t target:self selector:@selector(addButton:) userInfo:nil repeats:YES];

(void) addButton:(NSTimer*)timer{
   //Your code for adding button goes here 
   buttonCount++;
   if(buttonCount==20)
   {
          [timer invalidate];
          timer = nil;
          buttonCount = 0;
   }
}

Here 'buttonCount' is an integer variable that will keep the track of number of buttons added to the view. You can declare it in your header file.

Upvotes: 2

Related Questions