gpt4 book ai didi

ios - 方法在巨大延迟后更新 UI

转载 作者:行者123 更新时间:2023-11-30 13:04:53 24 4
gpt4 key购买 nike

我有一个 UIView 作为父 View ,有一个 UIActivityIndi​​cator 作为 subview 。每当用户提交凭据时,我都会启动 Activity 动画并在方法名称 startLoadingAnimator() 中分配parentViews alpha = 1.0,之后它会调用 API,当 API 完成调用时我设置通过停止 Activity 动画并在名为 stopLoadingAnimator() 的方法中设置父 View 的 alpha = 0.0 来恢复 Activity 动画。问题是 stopLoadingAnimator() 完全按时调用,但延迟后屏幕上显示的效果它应该就像方法运行时它应该在那一刻消失,但它需要很长时间才能消失。

Stops the activity animation.

func stopLoadingAnimator() -> Void {
UIView.animateWithDuration(0.25, animations: {

self.loadingView.alpha = 0
self.activityIndicator.stopAnimating()
})



}

Starts the activity animation.

func startLoadingAnimator() -> Void {
UIView.animateWithDuration(0.25, animations: {

self.loadingView.alpha = 1
self.activityIndicator.startAnimating()

})
}

Api Method

    func connectToWebWith(username:String, password:String) -> Void {
self.startLoadingAnimator()
let params = ["email":username, "password":password]

// params.setValue(username, forKey: "email")
// params.setValue(password, forKey: "password")

let request = NSMutableURLRequest(URL: NSURL(string: "https://callvabo.com/user/signin")!)
let session = NSURLSession.sharedSession()
request.HTTPMethod = "POST"

do {
request.HTTPBody = try NSJSONSerialization.dataWithJSONObject(params, options: .PrettyPrinted)
} catch {
self.stopLoadingAnimator()

//handle error. Probably return or mark function as throws
print(error)
return
}
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")

let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
// handle error
self.stopLoadingAnimator()
guard error == nil else {
return
}

print("Response: \(response)")
let strData = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("Body: \(strData)")

let json: NSDictionary?
do {
json = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableLeaves) as? NSDictionary
} catch let dataError {
// Did the JSONObjectWithData constructor return an error? If so, log the error to the console
print(dataError)
let jsonStr = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("Error could not parse JSON: '\(jsonStr)'")
// return or throw?
return
}


// The JSONObjectWithData constructor didn't return an error. But, we should still
// check and make sure that json has a value using optional binding.
if let parseJSON = json {
// Okay, the parsedJSON is here, let's get the value for 'success' out of it
let success = parseJSON["success"] as? Int
print("Succes: \(success)")
}
else {
// Woa, okay the json object was nil, something went worng. Maybe the server isn't running?
let jsonStr = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("Error could not parse JSON: \(jsonStr)")
}

})

task.resume()
}

最佳答案

更新您的开始和停止动画方法,以便它们始终在主线程上执行,如下所示:

Stops the activity animation.

 func stopLoadingAnimator() -> Void {
dispatch_async(dispatch_get_main_queue(), ^(){
//Add method, task you want perform on mainQueue
//Control UIView, IBOutlet all here

UIView.animateWithDuration(0.25, animations: {
self.loadingView.alpha = 0
self.activityIndicator.stopAnimating()
})
})
}

Starts the activity animation.

func startLoadingAnimator() -> Void {
dispatch_async(dispatch_get_main_queue(), ^(){
//Add method, task you want perform on mainQueue
//Control UIView, IBOutlet all here

UIView.animateWithDuration(0.25, animations: {
self.loadingView.alpha = 1
self.activityIndicator.startAnimating()
})
})
}

关于ios - 方法在巨大延迟后更新 UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39515424/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com