Animation Swift 3如何在按钮中设置字体动画
我有一个按钮,随着动画越来越大,越来越小。如何使字体随着按钮变大变小。我的代码如下Animation Swift 3如何在按钮中设置字体动画,animation,swift3,xcode8,font-size,Animation,Swift3,Xcode8,Font Size,我有一个按钮,随着动画越来越大,越来越小。如何使字体随着按钮变大变小。我的代码如下 func animate() { let originalFrame = self.playButton.frame let originalBackgroundColor = self.playButton.backgroundColor UIView.animateKeyframes(withDuration: 2, delay: 0, options: UIViewKeyfram
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)
}
请研究如何设置
变换
属性的动画
差不多
self.playButton.transform = CGAffineTransform.init(scaleX: 1.2, y: 1.2)
您可以存储原始字体大小,并按照与边框和背景色类似的方式缩放:
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 ...
}
让originalFontSize=self.playButton?.font.pointSize
:UIButton.font不再工作。UIButton
本身没有字体属性,但是UIButton.titleLabel有字体属性。