ios 如何隐藏和显示不带动画的CATextLayers

ymdaylpp  于 2023-06-07  发布在  iOS
关注(0)|答案(1)|浏览(110)

我有多个CATextLayers正在添加到一个AVVideoComposition。我想在视频中的特定时间点隐藏并显示它们。因此,我有以下代码:

CATransaction.begin()
CATransaction.setDisableActions(true)

let startVisible = CABasicAnimation(keyPath: "opacity")
startVisible.fromValue = 0.0
startVisible.toValue = 1.0
startVisible.beginTime = startTime
startVisible.duration = 0
startVisible.fillMode = .forwards
startVisible.isRemovedOnCompletion = false
textLayer.add(startVisible, forKey: "startAnimation")
CATransaction.commit()

CATransaction.begin()
CATransaction.setDisableActions(true)
let endVisible = CABasicAnimation(keyPath:"opacity")
endVisible.fromValue = 1.0
endVisible.toValue = 0.0
endVisible.beginTime = endTime
endVisible.duration = 0
endVisible.fillMode = .forwards
endVisible.isRemovedOnCompletion = false
textLayer.add(endVisible, forKey: "endAnimation")

CATransaction.commit()

这是可行的,但是,尽管CATransaction.setDisableActions(true),它确实会在不透明度级别之间设置淡入淡出动画。我怎样才能改变这一点,使不透明度的变化是即时的,而不是动画?

gmxoilav

gmxoilav1#

我找到的解决方案是将持续时间设置为很小的量。001,而不是0。不需要CATransaction函数。

相关问题