2017-02-22 43 views
0

我有一個隨着動畫變得越來越小的按鈕。如何讓字體隨着按鈕變得越來越小。我的代碼如下。Swift 3如何在按鈕中動畫字體

func animate() { 
    let originalFrame = self.playButton.frame 

    let originalBackgroundColor = self.playButton.backgroundColor 

    UIView.animateKeyframes(withDuration: 2, delay: 0, options: UIViewKeyframeAnimationOptions.calculationModeLinear, animations: { 

     // make the button grow and become dark gray 
     UIView.addKeyframe(withRelativeStartTime: 0.0, relativeDuration: 0.5) { 
      self.playButton.frame.size.height = self.playButton.frame.size.height * 1.2 
      self.playButton.frame.size.width = self.playButton.frame.size.width * 1.2 

      self.playButton.frame.origin.x = self.playButton.frame.origin.x - (self.playButton.frame.size.width/12) 
      self.playButton.frame.origin.y = self.playButton.frame.origin.y - (self.playButton.frame.size.height/12) 
     } 

     // restore the button to original size and color 
     UIView.addKeyframe(withRelativeStartTime: 0.5, relativeDuration: 0.5) { 
      self.playButton.frame = originalFrame 
      self.playButton.backgroundColor = originalBackgroundColor 
     } 
    }, completion: nil) 
} 

回答

1

可以存儲搶原來的字體大小和規模類似你如何與框架和背景顏色做到這一點:

func animate() { 

    // Original frame and background color 
    let originalFontSize = self.playButton.titleLabel?.font.pointSize 

    // First UIView.addKeyframe() 
    UIView.addKeyframe(withRelativeStartTime: 0.0, relativeDuration: 0.5) { 
     self.playButton.titleLabel?.font = UIFont.systemFont(ofSize: originalFontSize!*1.2) 
     // Other animations ... 
    } 

    // Second UIView.addKeyframe() to return to normal 
    UIView.addKeyframe(withRelativeStartTime: 0.5, relativeDuration: 0.5) { 
     self.playButton.titleLabel?.font = UIFont.systemFont(ofSize: originalFontSize!) 
     // Other animations ... 

} 
+0

'讓originalFontSize =自我。 playButton?.font.pointSize':UIButton.font不起作用了。 –

+0

'UIButton'本身沒有字體屬性,但是'UIButton.titleLabel'。 –

+0

我犯了一個錯誤,它效果很好。謝謝! –

0

調查動畫transform屬性代替。

喜歡的東西

self.playButton.transform = CGAffineTransform.init(scaleX: 1.2, y: 1.2)