【问题标题】:Swift: Two animations once after anotherSwift:一个接一个的两个动画
【发布时间】:2018-09-30 08:59:50
【问题描述】:

我遇到了这个小动画问题,我被卡住了。 我想显示两个动画,一个接一个(首先将卡片从按钮刷到中心,然后翻转它)。 所以我使用 UIView.animate 方法中的完成来触发第二个动画,一旦第一个动画完成。但在模拟器中,它每次只显示第二个动画。有人可以帮忙吗? 非常感谢

UIView.animate(withDuration: 1, delay: 0, options: .curveEaseOut, animations: {
    let animation:CABasicAnimation = CABasicAnimation(keyPath: "position")
    animation.fromValue = NSValue(cgPoint:CGPoint(x: self.imgBackOfTheCard.frame.midX, y: 1200))
    animation.toValue = NSValue(cgPoint:CGPoint(x: self.imgBackOfTheCard.frame.midX, y: self.imgBackOfTheCard.frame.midY))
    animation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
    animation.duration = 1
    self.imgBackOfTheCard.layer.add(animation, forKey: "position")
    }, completion: { (finished: Bool) in
        UIView.animate(withDuration: 1, delay: 1, options: .curveLinear, animations: {
            let transitionOptionsLeft: UIView.AnimationOptions = [.transitionFlipFromLeft, .showHideTransitionViews]
            UIView.transition(from: self.imgBackOfTheCard, to: self.imgFrontCard, duration: 1, options: transitionOptionsLeft, completion: nil)
        })
})

【问题讨论】:

  • 删除补全。第一个动画有效吗?

标签: swift swift4 swift4.2


【解决方案1】:

您正在混合 CA 动画和 UIView 动画。

第一个动画是CABasicAnimationUIView.animate 无法识别它,因此第一个动画立即“完成”(就UIView.animate 而言)并调用完成闭包。现在你再做一个UIView 动画。但实际上,第一个动画才刚刚开始,所以第二个动画接管了,你只能看到第二个。

要解决此问题,您应该将第一个动画移出 UIView.animate 块:

let animation:CABasicAnimation = CABasicAnimation(keyPath: "position")
animation.fromValue = NSValue(cgPoint:CGPoint(x: self.imgBackOfTheCard.frame.midX, y: 1200))
animation.toValue = NSValue(cgPoint:CGPoint(x: self.imgBackOfTheCard.frame.midX, y: self.imgBackOfTheCard.frame.midY))
animation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
animation.duration = 1
self.imgBackOfTheCard.layer.add(animation, forKey: "position")

然后等待1秒,再做第二个动画:

DispatchQueue.main.asyncAfter(deadline: .now() + 1, execute: {
   // do your second animation here.
})

经过进一步检查,第一个动画不一定是CABasicAnimation。它也可以使用UIView.animate 创建。只需设置frame 属性即可。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-12
    • 2018-01-24
    • 1970-01-01
    • 1970-01-01
    • 2011-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多