2017-04-18 42 views
0

我嘗試動畫化的UILabel:怪異的行爲CGAffineTransform

let label: UILabel = UILabel() 
var transform = CGAffineTransform.identity 

UIView.animate(withDuration: 2, animations: { 
    self.transform = self.transform.translatedBy(x: 0, y: -150) 
    self.transform = self.transform.scaledBy(x: 2, y: 2) 
    self.label.transform = self.transform 
}) 

它運作良好。我有一個按鈕,使被按下時,標籤再次動畫我增加了一個選擇:

func performSearch() { 
    UIView.animate(withDuration: 2, animations: { 
     self.transform = self.transform.translatedBy(x: 0, y: -300) 
     self.label.transform = self.transform 
    }) 
} 

但其實際作用是將標籤縮小爲原來的狀態,其向下移動到屏幕底部,然後動畫,雖然我想要做的就是向上移動。爲什麼?

回答

0

轉換可能有點令人困惑...因爲您有縮放轉換,進一步的轉換似乎是通過縮放「自動調整」,但不是以非常直觀或明顯的方式。

這可能會幫助你進一步得到:

// initial animation 
UIView.animate(withDuration: 2, animations: { 
    self.transform = self.transform.translatedBy(x: 0, y: -150) 
    self.transform = self.transform.scaledBy(x: 2, y: 2) 
    self.label.transform = self.transform 
}) 

// "resuming" animation 
func performSearch() { 
    let distance: CGFloat = -300/2 
    self.transform = self.transform.translatedBy(x: 0, y: distance) 
    self.label.transform = self.transform 

    UIView.animate(withDuration: 2, animations: { 
     self.transform = self.transform.translatedBy(x: 0, y: distance) 
     self.label.transform = self.transform 
    }) 


} 
+0

非常感謝!你能解釋一下嗎,爲什麼你的代碼工作? – user7861999

0

如果你想把它移到了在performSearch(),你需要使用正的y值:

self.transform = self.transform.translatedBy(x: 0, y: 300) 
0

您可以翻轉座標系統的行爲就像這樣的iOS ...

layer.sublayerTransform = CATransform3DMakeScale(1.0f, -1.0f, 1.0f);