Example #1
0
        private void ReloopForInterfaceChange()
        {
            if (this.updating)
            {
                return;
            }
            UIImage single      = int.Parse(UIDevice.CurrentDevice.SystemVersion.Split('.')[0]) >= 7 ? this.TintedImage(this.AnimationImage) : this.AnimationImage;
            var     imgs        = this.AnimationImages;
            var     masterImage = this.MasterImage;

            if (this.MasterImage == null || imgs == null || imgs.Count == 0 || imgs.First().Size.Width != this.Frame.Width)
            {
                var  expectedWidth  = this.Frame.Width + SingleElementWidth;
                bool completeReloop = this.MasterImage == null;

                if (completeReloop)
                {
                    masterImage = new UIImage(single.CGImage);
                    while (masterImage.Size.Width - SingleElementWidth < expectedWidth)
                    {
                        masterImage = masterImage.AttachImageRight(single);
                    }
                }
                else
                {
                    if (masterImage.Size.Width - SingleElementWidth < expectedWidth)
                    {
                        while (masterImage.Size.Width - SingleElementWidth < expectedWidth)
                        {
                            masterImage = masterImage.AttachImageRight(single);
                        }
                    }
                    else
                    {
                        while (masterImage.Size.Width - SingleElementWidth > expectedWidth + SingleElementWidth)
                        {
                            masterImage = masterImage.CropByX(SingleElementWidth);
                        }
                    }
                }

                this.MasterImage = masterImage;

                if (imgs == null)
                {
                    imgs = new List <UIImage>();
                }
                else
                {
                    imgs.Clear();
                }

                var size    = new CGSize(this.Frame.Width, masterImage.Size.Height);
                var pixels  = single.Size.Width * single.CurrentScale;
                var anchorX = -Math.Abs(masterImage.Size.Width - size.Width);
                for (int i = 0; i <= pixels; i++)
                {
                    UIGraphics.BeginImageContextWithOptions(size, false, single.CurrentScale);
                    CGContext context = UIGraphics.GetCurrentContext();
                    if (context != null)
                    {
                        context.TranslateCTM(0, masterImage.Size.Height);
                        context.ScaleCTM(1, -1);

                        context.DrawImage(new CGRect(anchorX + i, 0.0, masterImage.Size.Width, masterImage.Size.Height), masterImage.CGImage);

                        UIImage result = UIGraphics.GetImageFromCurrentImageContext();

                        imgs.Add(result);
                    }

                    UIGraphics.EndImageContext();
                }
            }

            this.AnimationImages = imgs;

            if (this.theImageView == null)
            {
                this.theImageView = new UIImageView();
            }
            if (this.host == null)
            {
                this.host = new UIView(this.Bounds);
                this.host.BackgroundColor = UIColor.Clear;

                if (int.Parse(UIDevice.CurrentDevice.SystemVersion.Split('.')[0]) >= 7)
                {
                    // this.host.Layer.CornerRadius = this.Frame.Size.Height / 2.0f;
                }
                else
                {
                    this.host.Layer.CornerRadius = this.theImageView.Frame.Size.Height / 2;
                }
                this.host.ClipsToBounds = true;
            }

            this.theImageView.Layer.MasksToBounds = true;

            if (this.host.Superview != this)
            {
                this.AddSubview(this.host);
            }

            if (this.theImageView.Superview != this.host)
            {
                this.host.AddSubview(this.theImageView);
            }

            this.theImageView.AnimationImages = imgs.ToArray();

            if (this.theImageView.AnimationDuration != this.animationSpeed)
            {
                this.theImageView.AnimationDuration = this.animationSpeed;
            }

            this.LayoutImageView();

            if (!this.theImageView.IsAnimating)
            {
                this.theImageView.StartAnimating();
            }
        }