如何同时为UIButton的titleLabel的帧和alpha设置动画?

时间:2018-01-16 17:39:24

标签: ios swift animation core-animation ios-animations

我想为UIButton titleLabel let animSize = CAKeyframeAnimation(keyPath: "bounds") animSize.values = [btn1.layer.frame,CGRect(origin: btn1.layer.frame.origin, size: CGSize(width: 300, height: btn1.layer.frame.height))] animSize.duration = 3 animSize.isRemovedOnCompletion = false animSize.fillMode = kCAFillModeForwards btn1.layer.add(animSize, forKey: "bounds") let animAlpha = CABasicAnimation(keyPath: "opacity") animAlpha.fromValue = 0.0 animAlpha.toValue = 1.0 animAlpha.timeOffset = 1.0 animAlpha.isRemovedOnCompletion = false animAlpha.fillMode = kCAFillModeForwards btn1.titleLabel?.layer.add(animAlpha, forKey: "opacity") 的宽度和alpha设置动画。 alpha动画应在1秒后开始 我试图用Core Animation做到这一点:

UIView.animat

当我单独运行动画时,一切都在工作,但是当我一起运行时,只有第一个动画("界限")正在评估。我知道我可以用CAAnimationGroup e做到这一点,但在这种情况下,我将失去"延迟"选项。我也尝试将这些动画放在ele,time,_lat,_lon 0.0000000,2013-12-03T21:08:56Z,45.4852855,-122.6347885 0.0000000,2013-12-03T21:09:00Z,45.4852961,-122.6347926 0.2000000,2013-12-03T21:09:01Z,45.4852982,-122.6347897 但没有改变。我想同时运行这些动画,因此我无法使用完成块。

感谢您的任何建议。

2 个答案:

答案 0 :(得分:0)

您不会丢失UIView.animate的“延迟”选项。您可以使用两个动画块来实现相同的结果:

UIView.animate(withDuration: 3) {
    btn1.frame = CGRect(origin: btn1.frame.origin, size: CGSize(width: 300, height: btn1.frame.height))
}

UIView.animate(withDuration: 2, delay: 1, options: [], animations: {
    btn1.alpha = 1
}, completion: nil)

答案 1 :(得分:0)

您也可以通过将按钮的宽度约束挂钩为IBOutlet来尝试使用autolayout

 btn1.withCon.constant = 300 
 UIView.animate(withDuration: 3) {
    self.view.layoutIfNeeded()
}

UIView.animate(withDuration: 2, delay: 1, options: [], animations: {
    btn1.alpha = 1
}, completion: nil)