我有UIView
作为父级,UIActivityIndicator
作为子视图。每当用户提交凭据时,我都会启动活动动画并在方法名称startLoadingAnimator()
中分配parentViews alpha = 1.0 ,然后调用API,当API完成调用后,我将设置为还原通过停止活动动画并在名为stopLoadingAnimator()
的方法中设置父视图的alpha = 0.0。
问题是stopLoadingAnimator()
在其时间上完美地调用,但是在延迟之后它显示在屏幕上的效果
它应该像方法运行一样,它应该在那一瞬间消失,但它需要很长时间才能消失。
停止活动动画。
func stopLoadingAnimator() -> Void {
UIView.animateWithDuration(0.25, animations: {
self.loadingView.alpha = 0
self.activityIndicator.stopAnimating()
})
}
开始活动动画。
func startLoadingAnimator() -> Void {
UIView.animateWithDuration(0.25, animations: {
self.loadingView.alpha = 1
self.activityIndicator.startAnimating()
})
}
Api方法
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()
}
答案 0 :(得分:0)
更新你的开始和停止动画方法,以便它们总是在主线程上执行,如下所示:
停止活动动画。
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()
})
})
}
开始活动动画。
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()
})
})
}