我在 UIVIew 中以编程方式添加 20 个 UIButton,我要做的是在这些按钮上添加动画,以便第一个按钮出现在时间“t”,第二个按钮出现在时间“t+1”,依此类推。我曾尝试在延迟一段时间后添加按钮,但它不起作用,所有按钮将一次显示以供查看。
如果有解决方案,请告诉我。
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); }
最佳答案
您可以使用 NSTimer
来实现它。
您可以安排一个计时器,它会在每“t”时间后调用所需的方法,直到所有 20 个按钮都添加到 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;
}
}
此处“buttonCount”是一个整型变量,用于跟踪添加到 View 中的按钮数量。您可以在头文件中声明它。
关于iphone - 在 iOS 中添加多个 subview 的动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11362253/