更改标签文本时的延迟响应(Swift)

问题描述 投票:0回答:1

我的问题是,当imageFound == false时,它打印出“No Results !!!”立即,但标签文本需要15秒才能更改。我不知道为什么这是滞后但我需要帮助尝试i =将其降低到5秒或更短的范围内。

代码如下......

if let textFieldContent = textField.text{
        do {

            try WikiFaceRec.faceForPerson(textFieldContent, size: CGSize(width: 200, height: 250), completion: {(image:UIImage?, imageFound:Bool!) -> ()  in
                if imageFound == false{
                    self.faceImageView.alpha = 0
                    self.realLoadingLbl.text = "No Results Found. Check your spelling and try again."
                    print("NO RESULTS!!!!!")

                }
                if imageFound == true{
                   self.realLoadingLbl.alpha = 0
                    dispatch_async(dispatch_get_main_queue(), {() -> Void in
                        self.faceImageView.image = image

                        self.faceImageView.alpha = 1
                        WikiFaceRec.centerImageViewOnFace(self.faceImageView)
                    })

                }
            })
        } catch WikiFaceRec.WikiFaceError.CouldNotDownloadImage {
            print("Wikipedia not currently open")

        } catch {
            print("error")
            self.faceImageView.alpha = 0
            self.realLoadingLbl.text = "No Results Found. Check your spelling and try again."
            print("NO RESULTS")
        }
    }
    return true
}

以下代码中包含self.realLoadingLbl.text =“未找到结果。请检查拼写并重试。”是需要改变的部分。是的,再次“没有结果!!!”立即打印。

if imageFound == false{
    self.faceImageView.alpha = 0
    self.realLoadingLbl.text = "No Results Found. Check your spelling and try again."
    print("NO RESULTS!!!!!")
}
xcode swift uiimageview wikipedia lag
1个回答
2
投票

你需要处理与imageFound==false相似的true案例的dispatch_async案例:

if !imageFound {
    dispatch_async(dispatch_get_main_queue()) {
        self.faceImageView.alpha = 0
        self.realLoadingLbl.text = "No Results Found. Check your spelling and try again."
        print("NO RESULTS!!!!!")
        self.faceImageView.alpha = 0                      
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.