2016-09-15 66 views
0

我有一个UIView作为父母和UIActivityIndicator作为子视图。每当用户提交凭证,我开始活动动画,并在方法名称startLoadingAnimator()中分配父视图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() 
    } 
+3

切勿从后台线程更新UI。 – rmaddy

+0

是的,更新主线程上的UI。 – Santosh

+0

我想在完成API回调后立即更新UI。与Objective-C类似,我们更新完成块中的UI –

回答

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() 
     }) 
    }) 
    } 
相关问题