CABasicAnimation链接不起作用

时间:2016-08-22 10:16:11

标签: swift cabasicanimation

我在链接多个CABasicAnimation时遇到困难。我只想将按钮刻度调整到1.5倍大小,然后调整为0.5并逐渐消失(缩小时)。

问题是只有按比例放大才能恢复到原始状态。

我会使用CGAffineTransformMakeScale但它会在iOS 7中创建一个错误。在iOS 8& 9它工作正常

我尝试使用CAAnimationGroup但结果相同。

以下是代码:

    let scaleUp = CABasicAnimation(keyPath: "transform.scale")
    scaleUp.fromValue = 1
    scaleUp.toValue = 1.5
    scaleUp.duration = 0.2
    scaleUp.beginTime = 0
    scaleUp.removedOnCompletion = true
    self.myButton.layer.addAnimation(scaleUp, forKey: "up")

    let scaleDown = CABasicAnimation(keyPath: "transform.scale")
    scaleDown.fromValue = 1
    scaleDown.toValue = 0.5
    scaleDown.duration = 0.2
    scaleDown.beginTime = scaleUp.beginTime + scaleDown.duration
    scaleDown.removedOnCompletion = true
    self.myButton.layer.addAnimation(scaleDown, forKey: "down")

    let fade = CABasicAnimation(keyPath: "opacity")
    fade.fromValue = 1
    fade.toValue = 0
    fade.duration = 0.2
    fade.beginTime = scaleUp.beginTime + scaleDown.duration
    fade.removedOnCompletion = false
    self.myButton.layer.addAnimation(fade, forKey: "fade")

2 个答案:

答案 0 :(得分:3)

对我很有用,可以随意播放持续时间或其他动画属性。

我尝试将您的说明用于动画,但这并不意味着我已根据您的期望正确设置了所有属性。

Swift(2.x)

func addGrowShrinkAndFadeOutAnimationToView(viewToAnimate: UIView) {

    let easeInOutTiming = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)

    let viewScaleXAnimation = CAKeyframeAnimation(keyPath: "transform.scale.x")
    viewScaleXAnimation.duration = 0.4
    viewScaleXAnimation.values = [1.0 as Float, 1.5 as Float, 0.5 as Float]
    viewScaleXAnimation.keyTimes = [0.0 as Float, 0.5 as Float, 1.0 as Float]
    viewScaleXAnimation.timingFunctions = [easeInOutTiming, easeInOutTiming]
    viewToAnimate.layer.addAnimation(viewScaleXAnimation, forKey:"Grow, Shrink and Fade out_ScaleX")

    let viewScaleYAnimation = CAKeyframeAnimation(keyPath: "transform.scale.y")
    viewScaleYAnimation.duration = 0.4
    viewScaleYAnimation.values = [1.0 as Float, 1.5 as Float, 0.5 as Float]
    viewScaleYAnimation.keyTimes = [0.0 as Float, 0.5 as Float, 1.0 as Float]
    viewScaleYAnimation.timingFunctions = [easeInOutTiming, easeInOutTiming]
    viewToAnimate.layer.addAnimation(viewScaleYAnimation, forKey:"Grow, Shrink and Fade out_ScaleY")

    let viewOpacityAnimation = CAKeyframeAnimation(keyPath: "opacity")
    viewOpacityAnimation.duration = 0.4
    viewOpacityAnimation.values = [1.0 as Float, 1.0 as Float, 0.0 as Float]
    viewOpacityAnimation.keyTimes = [0.0 as Float, 0.5 as Float, 1.0 as Float]
    viewOpacityAnimation.timingFunctions = [easeInOutTiming, easeInOutTiming]
    viewToAnimate.layer.addAnimation(viewOpacityAnimation, forKey:"Grow, Shrink and Fade out_Opacity")

}

答案 1 :(得分:1)

您是否有特定原因要使用CA动画?我发现操纵约束更容易。我在故事板中添加了一个按钮,并为按钮的高度和宽度限制创建了出口。

@IBOutlet weak var okButton: UIButton!
@IBOutlet weak var widthConstraint: NSLayoutConstraint!
@IBOutlet weak var heightConstraint: NSLayoutConstraint!
var buttonWidth : CGFloat?
var buttonHeight : CGFloat?

override func viewDidLoad() {
    super.viewDidLoad()
    self.buttonHeight = self.heightConstraint.constant
    self.buttonWidth = self.widthConstraint.constant
}

@IBAction func okButtonTapped(button: UIButton) {
    let animationDuration = 2.0
    self.heightConstraint.constant = self.buttonWidth! * 2.0
    self.widthConstraint.constant = self.buttonHeight! * 2.0
    UIView.animateWithDuration(animationDuration, animations: {
       button.layoutIfNeeded()
    }) { (finished) in
            self.heightConstraint.constant = self.buttonWidth! * 0.5
            self.widthConstraint.constant = self.buttonHeight! * 0.5
            UIView.animateWithDuration(animationDuration, animations: {
                button.alpha = 0.0
                button.layoutIfNeeded()
                }, completion: { (finished) in
                   button.hidden = true
            })
    }
}