Ios 需要基于URLSession(Swift3)中的数据运行动画的帮助吗
我在这里束手无策。我已经尝试了我能想到的一切,但无法让它发挥作用 我觉得我在这里要做的应该很简单——但斯威夫特让这件事变得非常困难,我感到沮丧 最后,我要做的就是从web脚本中获取一段数据(它将返回一个浮点),然后使用该浮点设置UIView的动画 因此,为了简单起见,我们假设web脚本返回“50.0”。然后我想取这个数字,并设置UIView的动画,使其从0px到50px高 我已经读到,您无法从异步任务中获取数据,并且您基本上必须运行希望在闭包中使用该数据运行的任何任务。然而。。。当我尝试在闭包中运行动画时,Xcode拍打我的手腕,告诉我我正在尝试在不同的线程中修改AutoLayout属性。那我该怎么办 以下是我的代码,请尽我所能对其进行注释: 首先,我的ViewController中有一个紫色背景的UIView。我在这个UIView中添加了一个子视图,它是绿色的,并且将根据web脚本的返回值垂直增长,有效地生成一个“米”(见图)Ios 需要基于URLSession(Swift3)中的数据运行动画的帮助吗,ios,animation,asynchronous,swift3,Ios,Animation,Asynchronous,Swift3,我在这里束手无策。我已经尝试了我能想到的一切,但无法让它发挥作用 我觉得我在这里要做的应该很简单——但斯威夫特让这件事变得非常困难,我感到沮丧 最后,我要做的就是从web脚本中获取一段数据(它将返回一个浮点),然后使用该浮点设置UIView的动画 因此,为了简单起见,我们假设web脚本返回“50.0”。然后我想取这个数字,并设置UIView的动画,使其从0px到50px高 我已经读到,您无法从异步任务中获取数据,并且您基本上必须运行希望在闭包中使用该数据运行的任何任务。然而。。。当我尝试在闭包中
在iOS中,您不能在后台线程上更新UI。要更新UI,请在主线程中编写代码,如下所示:
DispatchQueue.main.async{
//update your UI here
}
private func getMeterHeight(deviceID: String) {
guard let URL = URL(string: "http://www.mywebsite.com/getLatestReading.php") else {return}
let request = NSMutableURLRequest(url: URL)
request.httpMethod = "POST"
request.addValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
let bodyObject: [String: String] = [
"deviceID": deviceID
]
request.httpBody = try! JSONSerialization.data(withJSONObject: bodyObject, options: [])
var responseString: NSString = ""
let task = URLSession.shared.dataTask(with: request as URLRequest) {
data, response, error in
if (error == nil) {
responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)!
//This is the value that I need to get out of this unescapable black hole
let oilMeterHeight = 175 * responseString.doubleValue;
DispatchQueue.main.async{
//update your UI here
UIView.transition(with: self.oilReading, duration: 1.0, animations: {self.oilReading.frame = CGRect(x: self.tankRep.bounds.origin.x, y: CGFloat(self.tankRep.bounds.height - CGFloat(oilMeterHeight)), width: 100, height: CGFloat(oilMeterHeight))}, completion: nil)
}
} else {
// Failure
print("URL Session Task Failed: %@", error!.localizedDescription);
}
}
task.resume()
}
在您的情况下,要使用web服务响应中的值get设置视图动画,请在获取结果后编写此块。如果api调用一切正常,则getMeterHeight方法应如下所示:
DispatchQueue.main.async{
//update your UI here
}
private func getMeterHeight(deviceID: String) {
guard let URL = URL(string: "http://www.mywebsite.com/getLatestReading.php") else {return}
let request = NSMutableURLRequest(url: URL)
request.httpMethod = "POST"
request.addValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
let bodyObject: [String: String] = [
"deviceID": deviceID
]
request.httpBody = try! JSONSerialization.data(withJSONObject: bodyObject, options: [])
var responseString: NSString = ""
let task = URLSession.shared.dataTask(with: request as URLRequest) {
data, response, error in
if (error == nil) {
responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)!
//This is the value that I need to get out of this unescapable black hole
let oilMeterHeight = 175 * responseString.doubleValue;
DispatchQueue.main.async{
//update your UI here
UIView.transition(with: self.oilReading, duration: 1.0, animations: {self.oilReading.frame = CGRect(x: self.tankRep.bounds.origin.x, y: CGFloat(self.tankRep.bounds.height - CGFloat(oilMeterHeight)), width: 100, height: CGFloat(oilMeterHeight))}, completion: nil)
}
} else {
// Failure
print("URL Session Task Failed: %@", error!.localizedDescription);
}
}
task.resume()
}