/// <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 override void Draw(CGRect rect)
        {
            base.Draw(rect);

            if (Element.ShadowType == ShadowType.None)
            {
                _gradientLayer?.RemoveFromSuperLayer();
                return;
            }

            if (Element.ShadowType == ShadowType.AcrylicTop)
            {
                _gradientLayer?.RemoveFromSuperLayer();
                NativeView.Layer.BackgroundColor = new CGColor(1, 1, 1);
                return;
            }

            if (NativeView.Layer.Sublayers != null &&
                NativeView.Layer.Sublayers.Length > 0 &&
                NativeView.Layer.Sublayers[0] is CAGradientLayer)
            {
                return;
            }

            var gradientsPoints = ComputationHelper.RadiusGradientToPoints(180);

            // Top shadow
            var startColor = Color.FromHex("30000000");
            var endColor   = Color.FromHex("00ffffff");

            if (Element.ShadowType == ShadowType.Bottom)
            {
                var tmpColor = startColor;
                startColor = endColor;
                endColor   = tmpColor;
            }

            _gradientLayer = new CAGradientLayer
            {
                Frame      = rect,
                StartPoint = gradientsPoints.StartPoint.ToPointF(),
                EndPoint   = gradientsPoints.EndPoint.ToPointF(),
                Colors     = new[]
                {
                    startColor.ToCGColor(),
                        endColor.ToCGColor(),
                },
            };

            NativeView.Layer.InsertSublayer(_gradientLayer, 0);
            _previousSize = Bounds.Size;
        }
Example #3
0
 protected override void OnDetached()
 {
     if (gradLayer != null)
     {
         gradLayer.RemoveFromSuperLayer();
     }
 }
Example #4
0
        protected override void OnFillChanged(Brush newValue)
        {
            base.OnFillChanged(newValue);

            if (_gradientLayer != null)
            {
                _gradientLayer.RemoveFromSuperLayer();
                _gradientLayer = null;
            }

            _rectangleLayer.FillColor = _Color.Clear.CGColor;

            var scbFill = newValue as SolidColorBrush;
            var lgbFill = newValue as LinearGradientBrush;

            if (scbFill != null)
            {
                _fillSubscription.Disposable =
                    Brush.AssignAndObserveBrush(scbFill, c => _rectangleLayer.FillColor = c);
            }
            else if (lgbFill != null)
            {
                _gradientLayer = lgbFill.GetLayer(Frame.Size);
                Layer.InsertSublayer(_gradientLayer, 0);                 // We want the _gradientLayer to be below the _rectangleLayer (which contains the stroke)
            }

            InvalidateMeasure();
        }
        public override void RemoveFromSuperview()
        {
            base.RemoveFromSuperview();

            _gradient.RemoveAllAnimations();
            _gradient.RemoveFromSuperLayer();
        }
        protected override void Dispose(bool disposing)
        {
            if (TextColor != null)
            {
                TextColor.Dispose();
                TextColor = null;
            }

            if (HighlightColor != null)
            {
                HighlightColor.Dispose();
                HighlightColor = null;
            }

            if (ButtonTintColor != null)
            {
                ButtonTintColor.Dispose();
                ButtonTintColor = null;
            }

            if (_HighlightLayer != null)
            {
                _HighlightLayer.RemoveFromSuperLayer();
                _HighlightLayer.Dispose();
            }

            if (Font != null)
            {
                Font.Dispose();
            }

            base.Dispose(disposing);
        }
        private void CreateGradient()
        {
            gradientLayer?.RemoveFromSuperLayer();

            var button   = Element as Button;
            var uiButton = Control as UIButton;

            if (uiButton.Frame.Width == 0 || uiButton.Frame.Height == 0)
            {
                return;
            }

            var topColor    = button.BackgroundColor;
            var bottomColor = ButtonGradientEffect.GetGradientColor(button);

            gradientLayer = Gradient.GetGradientLayer(topColor.ToCGColor(), bottomColor.ToCGColor(), uiButton.Frame.Width, uiButton.Frame.Height);
            Control.Layer.InsertSublayer(gradientLayer, 0);
        }
Example #8
0
        void SetGradient()
        {
            gradLayer?.RemoveFromSuperLayer();

            var xfButton    = Element as Button;
            var colorTop    = xfButton.BackgroundColor;
            var colorBottom = ButtonGradientEffect.GetGradientColor(xfButton);

            gradLayer = Gradient.GetGradientLayer(colorTop.ToCGColor(), colorBottom.ToCGColor(), (float)xfButton.Width, (float)xfButton.Height);

            Control.Layer.InsertSublayer(gradLayer, 0);
        }
Example #9
0
        protected override void OnDetached()
        {
            if (_visualElement != null)
            {
                _visualElement.SizeChanged -= OnSizeChanged;
            }

            if (_view != null)
            {
                _view.ClipsToBounds = _clipsToBounds;
            }

            _layer?.RemoveFromSuperLayer();
            _layer?.Dispose();
            _layer = null;

            _view          = null;
            _visualElement = null;

            System.Diagnostics.Debug.WriteLine($"Detached {GetType().Name} from {Element.GetType().FullName}");
        }
        private void DrawBackground()
        {
            try
            {
                _gradientLayer?.RemoveFromSuperLayer();

                if (!Element.BackgroundGradientColorFrom.HasValue ||
                    !Element.BackgroundGradientColorTo.HasValue)
                {
                    return;
                }

                var startColor = Element.BackgroundGradientColorFrom.GetValueOrDefault().ToCGColor();
                var endColor   = Element.BackgroundGradientColorTo.GetValueOrDefault().ToCGColor();

                var startX = .5;
                var startY = 0.0;
                var endX   = .5;
                var endY   = 1.0;

                if (Element.IsHorizontalGradientBackground)
                {
                    startX = 0;
                    startY = .5;
                    endX   = 1;
                    endY   = .5;
                }

                _gradientLayer = new CAGradientLayer
                {
                    StartPoint = new CGPoint(startX, startY),
                    EndPoint   = new CGPoint(endX, endY),
                    Frame      = UIScreen.MainScreen.Bounds, // used it instead of rect
                    Colors     = new CGColor[]
                    {
                        startColor,
                        endColor
                    }
                };

                NativeView.Layer.InsertSublayer(_gradientLayer, 0);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
        }
Example #11
0
        /// <summary>
        /// Removes the layer from view.
        /// </summary>
        /// <param name="view">View.</param>
        public static void RemoveLayerFromView(UIView view)
        {
            CAGradientLayer layerToRemove = null;

            foreach (CALayer aLayer in view.Layer.Sublayers)
            {
                if (aLayer is CAGradientLayer)
                {
                    layerToRemove = (CAGradientLayer)aLayer;
                }
            }

            if (layerToRemove != null)
            {
                layerToRemove.RemoveFromSuperLayer();
            }
        }
Example #12
0
 protected override void OnDetached()
 {
     gradLayer?.RemoveFromSuperLayer();
 }