【发布时间】:2021-05-13 17:11:28
【问题描述】:
我使用 40 张图片创建了核心动画(每张图片在 CALayer 中停留的时间正好是 3 秒)。
总持续时间 = 120 秒。每张照片在CABasicAnimation(keypath : "position.x") 的帮助下从左向右移动。现在我有一个 UISlider。我想在 CALayer 中播放核心动画时自动更新滑块的值。我使用 Timer 像这样更新滑块的值 time = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.updateSlideValue), userInfo: nil, repeats: true)
动画代码:
let blackLayer = CALayer()
blackLayer.frame = CGRect(x: -viewSize.width, y: 0, width: viewSize.width, height: viewSize.height)
let imageLayer = CALayer()
imageLayer.frame = CGRect(x: 0, y: 0, width: viewSize.width, height: viewSize.height)
imageLayer.contents = nextPhoto?.cgImage
blackLayer.addSublayer(imageLayer)
let animation = CABasicAnimation()
animation.keyPath = "position.x"
animation.fromValue = -viewSize.width
animation.toValue = (viewSize.width)/2
animation.duration = self.perImageLife
animation.speed = 1 // 1 is default actually
animation.beginTime = CACurrentMediaTime() + animationTimes[index-1] // animationTimes is an array like [0,3,6,9,.....]
animation.fillMode = CAMediaTimingFillMode.forwards
animation.isRemovedOnCompletion = false
animation.delegate = self
blackLayer.add(animation, forKey: "basic")
parentLayer.addSublayer(blackLayer)
但使用 Timer 更新并不是一个更好的主意。核心动画在没有 Timer 的情况下运行时如何自动更新滑块的值?
这是我项目的一小段录制视频。 https://drive.google.com/file/d/1SjaWJM_FEWp1TyR7fpQt8_IZdme_I2Vc/view?usp=sharing
【问题讨论】:
-
你能分享你的动画代码吗?您当然可以在图像动画旁边为
UISlider值设置动画。 -
感谢您的快速响应。我添加了动画代码。请再次检查。并帮助我。 @CSmith
-
如果您的动画继续,那么您可以使用 CADisplayLink :developer.apple.com/documentation/quartzcore/cadisplaylink
标签: ios swift core-animation calayer cabasicanimation