2016-04-28 73 views
0

幾個小時我一直試圖解決最小的動畫故障。我的代碼成功地將視圖從屏幕上移開,然後將其再生成動畫。它獲得了x座標,但Y軸的行爲我不明白。下面的代碼:iOS動畫故障,Y座標問題

func listTrans_slideIn (slideFrom: String) { 
    //var newFrame = tableView.frame 
    tableView.frame.origin.x = 1000 
    //tableView.frame.origin.y = 100 
    print("Table pushed to side") 

    UIView.animateKeyframesWithDuration(1.375 /*Total*/, delay: 0.0, options: UIViewKeyframeAnimationOptions.CalculationModeLinear, animations: { 
     UIView.addKeyframeWithRelativeStartTime(0.0, relativeDuration: 1/1, animations:{ 
      self.tableView_toTLG_Top.constant = 130 
      self.tableView_toSV_Left.constant = 0 
      self.tableView_toSV_Right.constant = 0 
      self.setupView() 

      //newFrame.origin.y = self.hdrBox.frame.height+50 
      //newFrame.origin.x = 0 
      //self.tableView.frame = newFrame 

      self.tableView.layoutIfNeeded() 
     }) 
     }, 
     completion: { finished in 
      if (!finished) { return } 

     }) 
} 

怪異的行爲是,如果我把正確的動畫關鍵幀y座標,它有太高,但隨後在正確的座標平息。如果我輸入的座標太低,它會進入正確的高度,但會降低。

正如你所看到的,我嘗試過使用框架和約束。我試過改變高度,我把它從屏幕上移開,但似乎沒有效果。

任何人都知道爲什麼我花了半天時間看到這個bug?

回答

0

事實證明,這是操作問題的順序。解決方案要求我將動畫在viewDidAppear中進行。

其他說明:

  • 爲了使動畫看起來儘可能光滑,考慮關閉SEGUE的動畫。
  • 此外請確保您正在主線程中運行segue調用和動畫,以便一切順利且毫不拖延地進行
0

你可以嘗試這樣的事:

// Use the same value here and below, it moves it out of the screen 
     self.tableView.center.y -= 500 

UIView.animateWithDuration(1.0, animations: {() -> Void in 

     // Then it comes back 
      self.tableView.center.y += 500 
    })