gpt4 book ai didi

swift - CABasicAnimation 链接不起作用

转载 作者:搜寻专家 更新时间:2023-11-01 06:18:29 25 4
gpt4 key购买 nike

我在链接多个 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")

最佳答案

这对我来说效果很好,可以随意调整持续时间或其他动画属性。

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


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")

}

关于swift - CABasicAnimation 链接不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39076912/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com