Xamarin.Forms iOS使用图像创建动画

时间:2019-06-21 06:45:11

标签: xamarin.forms xamarin.ios cakeyframeanimation

我从事Xamarin.Fomrs共享项目。我想以100毫秒的间隔在同一位置显示多个图像。这样它看起来就像一个GIF。在Android中正在运行。我创建了可绘制文件,并在其中放置了所有带有时间间隔的图像。但是在iOS中,我遇到了问题。

我发现CAKeyFrameAnimation用于实现这种功能。但是我找不到想要的实现方法。

我已经在CAKeyFrameAnimation中实现了ImageRenderer

 class AnimatedImageRenderer : ImageRenderer
    {
        public AnimatedImageRenderer() { }
        Animation objAnimation = new Animation();
        protected override void OnElementChanged(ElementChangedEventArgs<Image> e)
        {
            base.OnElementChanged(e);

            if (Control != null)
            {
                try
                {
                    CAKeyFrameAnimation anim = CAKeyFrameAnimation.FromKeyPath("contents");

                    anim.Duration = 1;
                    //anim.KeyTimes = new[] {
                    //                NSNumber.FromDouble (0),    // FIRST VALUE MUST BE 0
                    //                NSNumber.FromDouble (0.1),
                    //                NSNumber.FromDouble(0.2),
                    //                NSNumber.FromDouble(0.3),
                    //                NSNumber.FromDouble(0.5),
                    //                NSNumber.FromDouble(0.6),
                    //                NSNumber.FromDouble(0.8),
                    //                NSNumber.FromDouble(1.0),   // LAST VALUE MUST BE 1
                    //                };
                    anim.Values = new NSObject[] {
                                    FromObject(UIImage.FromFile("bomb1.png")),
                                    FromObject(UIImage.FromFile("bomb2.png")),
                                    FromObject(UIImage.FromFile("bomb3.png")),
                                    FromObject(UIImage.FromFile("bomb4.png")),
                                    FromObject(UIImage.FromFile("bomb5.png")),
                                    FromObject(UIImage.FromFile("bomb6.png")),
                                    FromObject(UIImage.FromFile("bomb7.png")),
                                    };
                    //anim.TimingFunction = CAMediaTimingFunction.FromName(CAMediaTimingFunction.Linear);
                    anim.RepeatCount = 1;
                    anim.RemovedOnCompletion = false;
                    //anim.CalculationMode = CAAnimation.AnimationLinear;
                    //c.Init();
                    Control.Layer.AddAnimation(anim, "bomb");

                }
                catch (System.Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex.Message);
                    // ModCommon.TraceLog("AnimatedImageRenderer tm_Tick" + ex.Message);
                }
            }
        }
}

我不知道缺少什么额外的属性。请引导我。

谢谢:)

1 个答案:

答案 0 :(得分:0)

您应该使用CGImage而不是UIImage。将动画的值更改为:

anim.Values = new NSObject[] {
                FromObject(UIImage.FromFile("bomb1.png").CGImage),
                FromObject(UIImage.FromFile("bomb2.png").CGImage),
                FromObject(UIImage.FromFile("bomb3.png").CGImage),
                FromObject(UIImage.FromFile("bomb4.png").CGImage),
                FromObject(UIImage.FromFile("bomb5.png").CGImage),
                FromObject(UIImage.FromFile("bomb6.png").CGImage),
                FromObject(UIImage.FromFile("bomb7.png").CGImage),
                };