iphone - 将图像设置为按钮

标签 iphone cocoa cocoa-touch image button

有人可以帮助我解决以下问题吗?

当我调用 myFunction 时,我想要设置为按钮的图像会在 2 秒后同时出现,而不是延迟 0.5 秒一张一张地出现。

更多信息:

  • generatedNumbers 是包含 NSNumber 四个元素 (4,1,3,2) 的数组
  • 按钮通过 IB 在 UIView 中设置并标记为 (1,2,3,4)
<小时/>
-(IBAction) myFunction:(id) sender {
int i, value;
for (i = 0; i<[generatedNumbers count]; i++) {
    value = [[generatedNumbers objectAtIndex:i] intValue];
    UIButton *button = (UIButton *)[self.view viewWithTag:i+1];
    UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"%d.png",value]];
    [button setImage:img forState:UIControlStateNormal];
    [img release];
    usleep(500000);
}   
}

最佳答案

可能发生的情况是该方法正在主线程上运行。由于当您调用 usleep() 时它会等待完成,因此运行循环永远不会达到要更新 UI 的程度。尝试创建一个 NSTimer 并每 0.5 秒触发一个单独的方法;这应该给主线程足够的时间来更新。

关于iphone - 将图像设置为按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2768322/

相关文章:

ios - 复制Apple联系人编辑屏幕

ios - 试图检测在 scenekit 上点击了哪个节点

iOS9 - 无法验证任何应用程序

objective-c - NSTask 和 AuthorizationCreate

objective-c - NSBezierPath 上的剥离描边

ios - 从文档目录获取图像到 Collection View

iphone - 是否可以更改相机胶卷中的照片相册?

swift - NSSetUncaughtExceptionHandler 在 Swift 3 中没有触发?

ios - 如何让导航 Controller 在屏幕外加载时加载其 Root View Controller ?

cocoa-touch - 来自 UIImage 的数据 URL/PNG