2015-02-10 97 views
2

我試圖在我的測驗的下一個問題要加載之前讓按鈕「搖動」。如何等待動畫在Swift中完成?

動畫看起來像

var timer = NSTimer() 
    UIView.animateWithDuration(0.1, animations: { 
     self.IMGVIEWcat.center = CGPointMake(self.IMGVIEWcat.center.x + 2, self.IMGVIEWcat.center.y) 
    }) 
    UIView.animateWithDuration(0.2, animations: { 
     self.IMGVIEWcat.center = CGPointMake(self.IMGVIEWcat.center.x - 4, self.IMGVIEWcat.center.y) 
    }) 
    UIView.animateWithDuration(0.3, animations: { 
      self.IMGVIEWcat.center = CGPointMake(self.IMGVIEWcat.center.x + 2, self.IMGVIEWcat.center.y) 
    }) 

在此之後,下面的函數被調用

func QuerryInformations(){ 
     println("QuerryInformationsStart") 
     self.ActivityIndicator.hidden = false 
     ObjectIDsArrayCount = ObjectIDsArray.count 
     var RandomQuestion = Int(arc4random_uniform(ObjectIDsArray.count + 0)) 
     var QuestionID:String = ObjectIDsArray[RandomQuestion] 
     var query : PFQuery = PFQuery(className: "AddonQuiz") 
     query.getObjectInBackgroundWithId(QuestionID){ 
      (ObjectHolder : PFObject!, error : NSError!) -> Void in 
... 

現在動畫開始,但瞬間的下一個問題是加載,有沒有一些簡單的等待初學者實施「等待」直到動畫完成?

我試圖設置

var complet == true 

在動畫

並在查詢功能

if complet = true {.... 

但這並沒有爲我工作,我也發現了大約完成處理的一些信息但沒有得到它在我的代碼工作。

+0

'if complet = true {...}'不是你想要使用的。改用'if complet == true {...}'代替。順便說一句,你應該使用動畫結尾的'completion'塊。 – Cesare 2015-02-10 20:39:22

+0

true;)在代碼中它是==但它仍然沒有幫助 – 2015-02-10 20:40:03

回答

6

如果你想在動畫的最後使用completion塊,你應該使用結構與delayoptions參數以及。

UIView.animateWithDuration(0.3, delay: 0.0, options: UIViewAnimationOptions.CurveLinear, animations: { 
// put here the code you would like to animate 
    self.IMGVIEWcat.center = CGPointMake(self.IMGVIEWcat.center.x + 2, self.IMGVIEWcat.center.y) 

}, completion: {(finished:Bool) in 
// the code you put here will be compiled once the animation finishes 
    QuerryInformations() 
}) 
+0

在「}上得到錯誤,完成:{「'Bool'不是'()'的子類型 – 2015-02-10 20:43:42

+0

對不起,請重試。我編輯了代碼。 – Cesare 2015-02-10 20:44:40

+0

感謝作品現在:)! – 2015-02-10 20:45:40

0

上使用該功能的completion塊你打電話:

UIView.animateWithDuration(0.3, animations: { 
     self.IMGVIEWcat.center = CGPointMake(self.IMGVIEWcat.center.x + 2, self.IMGVIEWcat.center.y) 
}, completion: {(finished:Bool) in 
    QuerryInformations() 
}) 
+0

這目前給我一個錯誤,「延遲失蹤論」 – 2015-02-10 20:39:10

+0

它不應該。直接從https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/#//apple_ref/occ/clm/UIView/animateWithDuration:animations:completion: – 2015-02-10 20:49:55