CABasicAnimation swift 不工作
Posted
技术标签:
【中文标题】CABasicAnimation swift 不工作【英文标题】:CABasicAnimation swift not working 【发布时间】: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")
【问题讨论】:
【参考方案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 内容以上是关于CABasicAnimation swift 不工作的主要内容,如果未能解决你的问题,请参考以下文章
Swift : 为 CALayer 暂停 CABasicAnimation
Swift Playground 中的 CABasicAnimation