我有 2 个 CALayers
,每个都有一个图像。两者的初始 Opacity
均为 0
。
我想在 1 秒内将 Layer1 的 Opacity
动画化为 1
,立即开始。然后在延迟 0.5 秒后,我想在 1 秒内将 Layer2 的 Opacity
动画化为 1
。这两层位于彼此之上。
我想要实现的是让第一张图片淡入,然后在淡入时将第二张图片淡入。
但出于某种原因我不能使用 UIView.Animate
,因为它根本没有动画,只是直接设置值。
UIView.Animate(1, 0, UIViewAnimationOptions.CurveEaseIn, () =>
{
backgroundLayer.Opacity = 1;
}, () => UIView.Animate(5, () =>
{
backgroundLayer2.Opacity = 1;
}));
这里只是尝试一个接一个地直接运行动画,它仍然只是立即设置值,没有动画。
最佳答案
UIView 动画旨在为 UIView 属性设置动画,似乎不能很好地与 CALayers 一起播放。使用 UIImageViews 而不是 CALayers 将解决您的问题(那么您应该使用 imageView.alpha 属性而不是 layer.opacity 属性)。
但是,如果您坚持使用 CALayers,您可以使用 CABasicAnimation 为它们设置动画。下面的代码完成了您描述的动画(请注意,这是 Swift 代码,因为我不使用 Xamarin)。
var animation1 = CABasicAnimation(keyPath: "opacity")
// Don't go back to the initial state after the animation.
animation1.fillMode = kCAFillModeForwards
animation1.removedOnCompletion = false
// Set the initial and final states of the animation.
animation1.fromValue = 0
animation1.toValue = 1
// Duration of the animation.
animation1.duration = 1.0
var animation2 = CABasicAnimation(keyPath: "opacity")
animation2.fillMode = kCAFillModeForwards
animation2.removedOnCompletion = false
animation2.fromValue = 0
animation2.toValue = 1
animation2.duration = 1.0
// Add a 0.5 second delay.
animation2.beginTime = CACurrentMediaTime() + 0.5
// Animate!
backgroundLayer.addAnimation(animation1, forKey: "opacity")
backgroundLayer2.addAnimation(animation2, forKey: "opacity")
关于c# - 使用 UIView.Animate 动画图层不透明度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30396711/