[英]MonoTouch - UIView.Animate completion callback is not called inside button touchUpInside delegate
[英]Animate Layer Opacity with UIView.Animate
我有2個CALayers
,每個都有一個圖像。 兩者的初始Opacity
均為0
。
我想層1的動畫Opacity
,以1
比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")
您只能使用UIView.Animate為以下屬性設置動畫:
對於更復雜的動畫,您必須像@bjornorri一樣使用CABasicAnimation。
你們是正確的,因為我試圖使用不正確的方法為圖層設置動畫。 我最終做的是用UIImageView
替換圖層並使用UIView.Animate
來更改Alpha
屬性。
這不僅更容易編碼,看起來UIImageView
在圖像方面實際上更具性能。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.