iphone - 在 iOS 中添加多个 subview 的动画

标签 iphone ios xcode core-animation

我在 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/

相关文章:

iphone - 可编辑的表格 View 单元格

iphone - 使用 iPhone 相机测量距离

ios - ios 7 中的状态栏问题

ios - 将 View Controller 导入另一个 Controller 以进行 segueing 是否安全?

swift - 尝试使用 Firebase 保存和获取数据但收到此错误

ios - 在 Swift 中用包含时间的字符串替换当前日期

ios - 自动布局中的中心 Storyboard View

ios - 我的 TableViewController 卡住了(初学者)

swift - 代码。添加到测试目标的图像资源不会复制到测试包中

objective-c - 从 Swift 类调用 Objective-C 导致链接器错误