作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 StackOverflow 上通读了大约 15 篇讨论 UIImageViews 的主题并为它们设置动画以交叉淡入淡出的帖子。我最终想要交叉淡入淡出,但此时我什至无法更改图像。
我的代码是
var next = 0
let names = ["image_1", "image_2", "image_3", "image_4"]
// While the image is still in memory, keep cycling every few seconds
while ImageView != nil {
print(next % 4)
// Grab the new image
let nextImage = UIImage(named: names[next % 4])
next++
// Set the new image
ImageView.image = nextImage
// Wait a few seconds
NSThread.sleepForTimeInterval(NSTimeInterval(4))
}
图像名称来 self 的 xcassets。当我运行它时,图像没有改变,但循环仍然每隔几秒打印一次
我做错了什么?
最佳答案
你正在主线程中运行这个循环
因此循环阻塞了您的 MainThread 并且无法更新 ImageView。
当您的代码被点击时,UI-Stuff 不会立即更新。
你可以这样做:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
while ImageView != nil {
print(next % 4)
let nextImage = UIImage(named: names[next % 4])
next++
dispatch_async(dispatch_get_main_queue()) {
// UI Changes need to be done in the main thread
ImageView.image = nextImage
}
// blocking code needs to be done in another thread
NSThread.sleepForTimeInterval(NSTimeInterval(4))
}
}
关于ios - UIImageViewImage 在设置时不会改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37600044/
我在 StackOverflow 上通读了大约 15 篇讨论 UIImageViews 的主题并为它们设置动画以交叉淡入淡出的帖子。我最终想要交叉淡入淡出,但此时我什至无法更改图像。 我的代码是 va
我是一名优秀的程序员,十分优秀!