2017-02-17 37 views
2

有沒有人在試圖用CGAffineTransform旋轉動畫形狀對稱的UIBarButton幾次時看到一種情況,然後結束了按鈕變形?CGAffineTransform在幾次旋轉後讓UIBarButton變形

optionButton.transform = CGAffineTransform(rotationAngle: CGFloat(M_PI)) 

enter image description here

enter image description here

+0

您是否檢查過此按鈕是否添加了自動調整遮罩或自動佈局約束? – user3581248

+0

@Giraffe,檢查此按鈕的「UIImageView」的contentMode。嘗試將它設置爲.scaleAspectFit – KrishnaCA

+0

@ user3581248我的自定義UIButton已添加到UINavigationBar的customView圖層,因此我不覺得它是由於使用了錯誤的約束而導致的。 – Giraffe

回答

1

由UIButton的身份矩陣重置身份,似乎來解決這個問題。但爲什麼?

UIView.animate(withDuration: 0.20, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: UIViewAnimationOptions.curveEaseIn, animations: { 
      if let optionsButton = self.homeController?.optionsButton { 
       optionsButton.transform = optionsButton.transform.rotated(by: CGFloat(-M_PI_2)) 
      } 
     }) { (finished: Bool) in 
      if let optionsButton = self.homeController?.optionsButton { 
       optionsButton.transform = CGAffineTransform.identity 
      } 
} 

好了,所以正確的&簡短的回答是,我忘了來定義我的UIButton的將backgroundImage的「contentMode」。

button.contentMode = .scaleAspectFit 

如果我錯了,請糾正我。長的回答:

引用UIButton的UIView圖層的單位矩陣圖層不是正方形(如果它們只有幾個像素的差異,可能很難發現形狀上的差異。用戶界面調試工具可以提供幫助。)當按鈕動畫本身時,較長的一面在單位矩陣層(不是UIView)中將短邊換成較短的一邊。因此,如果我繼續使用這個UIButton進行動畫處理,「矩形」的兩邊將會變長,然後最終拉長。重置UIButton的單位矩陣是偶然的,因爲矩陣層被重置回原來的位置,但這只是一個拙劣的解決方案。