Пример #1
0
        public static CAGradientLayer BuildShimmeringGradient(
            CGRect bounds,
            int repeatCount = 3)
        {
            CAGradientLayer  gradient  = new CAGradientLayer();
            CABasicAnimation animation = new CABasicAnimation()
            {
                KeyPath = "locations"
            };

            gradient.Colors = new CGColor[]
            {
                ShimmerAnimationConfig.DarkColor,
                ShimmerAnimationConfig.LightColor,
                ShimmerAnimationConfig.DarkColor
            };
            gradient.Frame        = new CGRect(-2 * bounds.Size.Width, 0, 4 * bounds.Size.Width, bounds.Size.Height);
            gradient.StartPoint   = new CGPoint(0, 0.5);
            gradient.EndPoint     = new CGPoint(1, 0.5);
            gradient.Locations    = new Foundation.NSNumber[] { 0.4, 0.5, 0.6 };
            animation.Duration    = ShimmerAnimationConfig.Duration;
            animation.RepeatCount = repeatCount;
            animation.SetTo(NSArray.FromObjects(0.0, 0.12, 0.13));
            animation.SetFrom(NSArray.FromObjects(0.6, 0.86, 1.0));
            gradient.AddAnimation(animation, shimmerKey);

            return(gradient);
        }
        /// <summary>
        /// Adds a gradient layer as a background layer to a view.
        /// </summary>
        /// <param name="view">The view to add the gradient to.</param>
        /// <param name="gradient">A gradient layer.</param>
        /// <param name="index">The index to insert the gradient layer (default=0)</param>
        public static void AddGradientBackground(this UIView view, CAGradientLayer gradient, int index = 0, bool animate = false)
        {
            gradient.Frame         = view.Bounds;
            gradient.MasksToBounds = true;
            CAGradientLayer existingGradient = null;

            if (view.Layer.Sublayers != null)
            {
                foreach (var l in view.Layer.Sublayers)
                {
                    if (l.GetType() == typeof(CAGradientLayer))
                    {
                        existingGradient = (CAGradientLayer)l;
                        break;
                    }
                }
            }
            view.Layer.InsertSublayer(gradient, index);
            gradient.Opacity = animate ? 0 : 1;
            if (existingGradient != null)
            {
                existingGradient.Opacity = 1;
                view.Layer.InsertSublayer(gradient, index + 1);
                if (animate)
                {
                    // Create fade in animation for the new gradient
                    var fadein = CABasicAnimation.FromKeyPath("opacity");
                    fadein.SetTo(NSNumber.FromFloat(1));
                    fadein.Duration            = .5;
                    fadein.AutoReverses        = false;
                    fadein.RemovedOnCompletion = false;
                    fadein.FillMode            = CAFillMode.Forwards;
                    // Create fade out animation for the existing gradient
                    var fadeout = CABasicAnimation.FromKeyPath("opacity");
                    fadeout.SetTo(NSNumber.FromFloat(0));
                    fadeout.Duration            = .5;
                    fadeout.AutoReverses        = false;
                    fadeout.RemovedOnCompletion = false;
                    fadeout.FillMode            = CAFillMode.Forwards;
                    fadeout.AnimationStopped   += (sender, e) =>
                    {
                        existingGradient.RemoveFromSuperLayer();
                    };
                    // Execute the animations.
                    gradient.AddAnimation(fadein, "opacityIN");
                    existingGradient.AddAnimation(fadeout, "opacityOUT");
                }
                else
                {
                    existingGradient.RemoveFromSuperLayer();
                }
            }
        }
        public void AnimateGradient()
        {
            _currentGradient += 1;
            var animation = CABasicAnimation.FromKeyPath(KeyPath);

            animation.Duration = AnimationDuration;

            var nsArray = NSArray.FromObjects(CurrentGradientSet());

            animation.To = nsArray;
            animation.RemovedOnCompletion = false;

            animation.AnimationStopped += (sender, e) => {
                _gradient.Colors = CurrentGradientSet();
                AnimateGradient();
            };

            animation.FillMode = CAFillMode.Forwards;

            _gradient.AddAnimation(animation, Key);
        }
Пример #4
0
        public static void ApplyShimmer(UIView viewToApplyShimmer)
        {
            var gradientLayer = new CAGradientLayer
            {
                Colors     = new CGColor[] { UIColor.White.ColorWithAlpha(0f).CGColor, UIColor.White.ColorWithAlpha(1f).CGColor, UIColor.White.ColorWithAlpha(0f).CGColor },
                StartPoint = new CGPoint(0.7, 1.0),
                EndPoint   = new CGPoint(0, 0.8),
                Frame      = viewToApplyShimmer.Bounds
            };

            viewToApplyShimmer.Layer.InsertSublayer(gradientLayer, 0);

            var animation = new CABasicAnimation();

            animation.KeyPath     = "transform.translation.x";
            animation.Duration    = 1;
            animation.From        = NSNumber.FromNFloat(-viewToApplyShimmer.Frame.Size.Width);
            animation.To          = NSNumber.FromNFloat(viewToApplyShimmer.Frame.Size.Width);
            animation.RepeatCount = float.PositiveInfinity;

            gradientLayer.AddAnimation(animation, "");
        }