【问题标题】:CABasicAnimation swift not workingCABasicAnimation swift 不工作
【发布时间】:2016-09-28 10:22:49
【问题描述】:

我刚从 obj C 迁移到 swift,我不确定为什么它不起作用,因为在 obj c 中它工作正常,请查看我的代码

这是我的代码

func addAnimationOnLayer( layer: CALayer, position: CGPoint, duration: TimeInterval, delay: TimeInterval, fromPosition: CGPoint, toPostion: CGPoint, key: String) {
        layer.setAffineTransform(CGAffineTransform(translationX: position.x, y: position.y))
        CATransaction.begin()
        CATransaction.setCompletionBlock {
            layer.setAffineTransform(CGAffineTransform(translationX: 0, y: 0))
        }
        let theAnimation = CABasicAnimation(keyPath: "transform.translation")
        theAnimation.isRemovedOnCompletion = false
        theAnimation.timingFunction = CAMediaTimingFunction(name:kCAMediaTimingFunctionEaseInEaseOut)
        theAnimation.fillMode = kCAFillModeForwards
        theAnimation.duration = duration
        theAnimation.beginTime = delay
        theAnimation.fromValue = fromPosition
        theAnimation.toValue = toPostion

        layer.add(theAnimation, forKey: key)
        CATransaction.commit()
    }

我用这个调用函数

self.addAnimationOnLayer(layer: self.logoImage.layer, position: CGPoint(x: 0, y:100), duration: 0.8, delay: 0.1, fromPosition: CGPoint(x: 0,y: 100), toPostion: CGPoint(x: 0,y: 0), key: "logoStartAnimation")

【问题讨论】:

    标签: swift cabasicanimation


    【解决方案1】:

    在致电CATransaction.begin() 之前,请尝试致电CATransaction.flush()

    您的代码将如下所示:

    func addAnimationOnLayer( layer: CALayer, position: CGPoint, duration: TimeInterval, delay: TimeInterval, fromPosition: CGPoint, toPostion: CGPoint, key: String) {
        layer.setAffineTransform(CGAffineTransform(translationX: position.x, y: position.y))
        CATransaction.flush()
        CATransaction.begin()
        CATransaction.setCompletionBlock {
            layer.setAffineTransform(CGAffineTransform(translationX: 0, y: 0))
        }
        let theAnimation = CABasicAnimation(keyPath: "transform.translation")
        theAnimation.isRemovedOnCompletion = false
        theAnimation.timingFunction = CAMediaTimingFunction(name:kCAMediaTimingFunctionEaseInEaseOut)
        theAnimation.fillMode = kCAFillModeForwards
        theAnimation.duration = duration
        theAnimation.beginTime = delay
        theAnimation.fromValue = fromPosition
        theAnimation.toValue = toPostion
    
        layer.add(theAnimation, forKey: key)
        CATransaction.commit()
    }
    

    试一试,让我知道,我之前遇到过动画没有发生的问题,因为之前的动画残留了一些。

    【讨论】:

    • 感谢您的帮助,动画似乎已损坏(快速移动/跳过)。即使我尝试删除动画仍然无法正常工作的 CATransaction 内容
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多