Swift 3如何在按钮中设置字体动画

时间:2017-02-22 22:24:21

标签: animation swift3 xcode8 font-size

我有一个随着动画变得越来越大的按钮。如何使字体随按钮变大和变小。我的代码如下。

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 个答案:

答案 0 :(得分:0)

请查看动画transform属性。

这样的东西
self.playButton.transform = CGAffineTransform.init(scaleX: 1.2, y: 1.2)