我正在使用一个具有旋转 body 动画的 iOS 应用程序。一系列 UIImage 存储在 NSArray 中,并且一个变量跟踪当前图像的索引。
以下方法更新 UIImageView
- (void)renewImage{
[rotatingBody.image release];
rotatingBody.image = [[UIImage alloc] initWithContentsOfFile:[currentPerson.imageArray objectAtIndex:pictureIndex]];
}
到目前为止,我已经调用该方法作为对触摸事件的响应。根据触摸事件,我计算一个新的 pictureIndex,然后调用 [self renewImage]。效果很好。
但是,现在我想为动画添加动力。我没有立即计算下一张图像,而是计算速度。我计划在单独的线程中创建一个循环,通过使用速度变量来更新 pictureIndex,降低速度并最终调用 [self renewImage]。
输入问题
每当我在同一个函数中连续调用 [self renewImage] 一次以上(例如,在循环中或通过某种 sleep 间隔两次),屏幕直到最后一次调用后才会更新。就好像设备(或编译器)合理化了第一次更新,即使我确信 renewImage 方法已被访问(感谢跟踪)。
这是一些不起作用的测试代码:
while (velocity > 0) {
pictureIndex++;
pictureIndex = pictureIndex % [self getNumberOfImages];
if (pictureIndex < 0)
{
pictureIndex += [self getNumberOfImages];
}
velocity -= 20;
NSLog(@"velocity: %f", velocity);
[self renewImage];
[NSThread sleepForTimeInterval:0.5];
}
这也不是:
pictureIndex += 20;
[self renewImage];
sleep(1);
pictureIndex += 20;
[self renewImage];
有什么想法吗?
最佳答案
使用 NSTimer 回调来更改图像,这样一切都发生在运行循环中,而不是在单独的线程上。
如果 NSTimer 不足以满足您的需求,您需要在主线程上调用一个方法来更改图像,因为其他线程上的 UI 更改不会立即更新。
关于objective-c - 重复更新 UIImage 未显示在屏幕上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7104358/