CABasicAnimation 链接不起作用

Posted

技术标签:

【中文标题】CABasicAnimation 链接不起作用【英文标题】:CABasicAnimation chaining not working 【发布时间】:2016-08-22 10:16:11 【问题描述】:

我在链接多个 CABasicAnimations 时遇到了困难。我只想将按钮缩放到其大小的 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")

【问题讨论】:

你想要一个有弹性的效果和褪色效果相辅相成吗? 是的。按钮按下 -> 放大 -> 缩小和淡出 所以...您的实际连锁店在哪里?我在这里只看到三个独立的动画。 我刚刚写了一些投票赞成的答案。如果您知道如何正确操作,请告诉我。 【参考方案1】:

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

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


斯威夫特 (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")


【讨论】:

谢谢加博!我做了必要的更改,效果很好。【参考方案2】:

您是否有特定原因要使用 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
            )
    

【讨论】:

感谢您的回答,但我真的不喜欢搞乱约束。我使用 CAAnimation 是因为我希望应用程序与 iOS 7 兼容,但 CGAffineTransformMakeScale 在 iOS 7 上无法正常工作,并且会产生错误。

以上是关于CABasicAnimation 链接不起作用的主要内容,如果未能解决你的问题,请参考以下文章

旋转 NSButton 不起作用

CABasicAnimation swift 不工作

一个 href 链接不起作用-当我单击链接 snimka1.html 时它不起作用,可能是许多 div 标签?-

Phonegap链接不起作用

社交图标在移动设备和某些平板电脑中不起作用(链接不起作用,它们不会在悬停时更改图像)

为啥用 textContent 链接不起作用