2017-02-22 70 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) 

 Смежные вопросы

  • Нет связанных вопросов^_^