public static BorderGrandientConfig Create(UIColor[] color, Orientation orientation) { var border = new BorderGrandientConfig { StartColor = color.FirstOrDefault()?.CGColor, EndColor = color.LastOrDefault()?.CGColor }; switch (orientation) { case Orientation.Vertical: border.StartPoint = new CGPoint(0f, 0.0f); border.EndPoint = new CGPoint(1.0f, 1.0f); break; case Orientation.Horizontal: border.StartPoint = new CGPoint(0f, 0.5f); border.EndPoint = new CGPoint(1.0f, 0.5f); break; default: throw new ArgumentOutOfRangeException(nameof(orientation), orientation, null); } return(border); }
public static void AddGradientBorder(this UIView view, BorderGrandientConfig config) { view.Layer.MasksToBounds = true; view.Layer.BorderWidth = 0f; var grandientLayer = new CAGradientLayer(); var bounds = new CGRect(CGPoint.Empty, view.Frame.Size); grandientLayer.Frame = bounds; grandientLayer.StartPoint = config.StartPoint; grandientLayer.EndPoint = config.EndPoint; grandientLayer.Colors = new[] { config.StartColor, config.EndColor }; var shapeLayer = new CAShapeLayer { LineWidth = config.BorderWidth, FillColor = null, StrokeColor = UIColor.Black.CGColor, Path = UIBezierPath.FromRoundedRect(bounds, view.Layer.CornerRadius).CGPath }; grandientLayer.Mask = shapeLayer; view.Layer.AddSublayer(grandientLayer); }