2012-07-06 53 views
0

我在UIVIew中以编程方式添加了20个UIButton,我要做的就是在这些按钮上添加动画,以便第一个按钮在时间t之后出现,时间t之后将出现在时间t +1“等。我曾尝试在延迟后添加按钮,但无法使用所有按钮将一次显示为查看。
如果有解决方案,请让我知道。在iOS中添加多个子视图的动画

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); 
} 
+0

你可以发布代码,尝试使用延迟来实现这一目标吗? – 2012-07-06 12:32:40

+0

看到编辑的问题... – 2012-07-06 13:14:48

+0

好吧,所以你添加延迟1秒为每个按钮添加子视图,以便他们都出现在同一时间。试试这个:[self.view performSelector:@selector(addSubview :) withObject:button afterDelay:i * 0.1]; – 2012-07-06 13:19:59

回答

2

您可以使用NSTimer来实现这一点。

您可以安排一个定时器,在每个't'时间后调用所需的方法,直到所有20个按钮都添加到视图中。

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

这里buttonCount'是整型变量,将保持添加到视图按钮的数量的轨道。你可以在头文件中声明它。

+0

thanx,那是伎俩 – 2012-07-06 16:38:39

+0

伟大的,它为你工作..你是欢迎.. :) – 2012-07-09 07:24:29