动画渐变背景

时间:2018-03-25 13:34:37

标签: c# xamarin.ios

我正在尝试为渐变设置动画,但我无法让它工作。我在这里错过了什么?我读了许多不同的指南,但它不会工作。我设法让其他动画工作,比如旋转。

        public override void ViewDidLoad()
    {

        base.ViewDidLoad();

        CGColor[] colors = new CGColor[] {
            new UIColor(red: 0.05f, green:0.44f, blue:0.73f, alpha:1.0f).CGColor,
            new UIColor(red: 45/255, green: 128 / 255, blue: 153 / 255, alpha: 0.01f ).CGColor
        };

        CAGradientLayer gradientLayer = new CAGradientLayer();
        gradientLayer.Frame = this.View.Bounds;
        gradientLayer.Colors = colors;

        var animat = CABasicAnimation.FromKeyPath("colors");
        animat.From = NSObject.FromObject(colors);
        animat.To = NSObject.FromObject(new CGColor[] {
            new UIColor(red:244/255, green: 88/255, blue: 53/255, alpha:1.0f).CGColor,
            new UIColor(red:196/255, green: 70/255, blue: 107/255, alpha: 0.01f ).CGColor
        });
        animat.Duration = 10;
        animat.FillMode = CAFillMode.Forwards;
        animat.RemovedOnCompletion = false;
        animat.TimingFunction = CAMediaTimingFunction.FromName(CAMediaTimingFunction.Linear);
        gradientLayer.AddAnimation(animat, "animateGradient");

        this.View.Layer.InsertSublayer(gradientLayer, 0); 
    }

1 个答案:

答案 0 :(得分:0)

您应该使用From修改ToNSArray属性,如:

animat.From = NSArray.FromObjects(colors);
animat.To = NSArray.FromObjects(new CGColor[] {
    new UIColor(red:244/255, green: 88/255, blue: 53/255, alpha:1.0f).CGColor,
    new UIColor(red:196/255, green: 70/255, blue: 107/255, alpha: 0.01f ).CGColor
});