Can't change UIImageView image in function (SWIFT)

前端 未结 2 863
你的背包
你的背包 2021-01-29 14:43

I have called this function after it is declared and everything works but the UIImageViews don\'t change their image. I would be very grateful for your help.

相关标签:
2条回答
  • 2021-01-29 15:35

    I think that the problem is the sleep(3), this line block the main thread. Instead of that use:

    DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
        // your code here
    }
    

    Apple Reference

    Note:
    For the most part, use UIKit classes only from your app’s main thread. This is particularly true for classes derived from UIResponder or that involve manipulating your app’s user interface in any way.

    Regards

    0 讨论(0)
  • 2021-01-29 15:50

    Not sure where you are updating your image either in main thread or background. Don't use sleep, Instead try updating your image in main thread asynchronously with delay.

    DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(3))
    { self.GreenImage.image = UIImage }
    
    0 讨论(0)
提交回复
热议问题