1

I have a rectangle bounds (10, 20, 100, 200) and the CGPoints are StartPoint (0.5, 0.5) and EndPoints as (1, 1). From these points how needs to calculate the segments bounds ? I need to apply this bounds for CGGradient for start point and end points.

Eg Code :

GradientColor gradientColor1 = new GradientColor(){StartPoint = new CGPoint(0.5, 0), EndPoint= new CGPoint(0.5, 1)};

GradientStop stop1 = new GradientStop() { Color = UIColor.Red, Offset = 0.1f };
GradientStop stop2 = new GradientStop() { Color = UIColor.Blue, Offset = 0.9f };

can you please help me out of this?

Parthiban
  • 168
  • 1
  • 13
  • rectangle bounds: (10, 20, 100, 200); Gradient points = StartPoint(0.5, 0) and EndPoints (0.5, 1); GradientStops1 : Color : UIColor.Red, Offset = 0.4; GradientStops2 : Color : UIColor.Blue, Offset = 0.6; – Parthiban Jan 08 '18 at 04:54
  • @SushiHangover Formated code updated in query. – Parthiban Jan 08 '18 at 05:03
  • GradientColor/GradientStop are Windows classes not iOS, what exactly are you trying to do with the gradient? Create a `CAGradientLayer`? – SushiHangover Jan 08 '18 at 05:10
  • I am trying with CGGradient in iOS? which are drawing customly in my context https://developer.xamarin.com/api/member/CoreGraphics.CGContext.DrawLinearGradient/ – Parthiban Jan 08 '18 at 05:12

1 Answers1

1

Here an an example that will create a left to right linear gradient within the current CGContext.

using (var context = UIGraphics.GetCurrentContext ()) {
    context.SaveState();
    var startPoint = new CGPoint(rect.Left, 0);
    var endPoint = new CGPoint(rect.Right, 0);
    var components = new CGColor[] { UIColor.Red.CGColor, UIColor.Blue.CGColor };

    using (var rgb = CGColorSpace.CreateDeviceRGB()) {
        var gradient = new CGGradient(rgb, components);
        context.DrawLinearGradient(gradient, startPoint, endPoint, CGGradientDrawingOptions.DrawsBeforeStartLocation);
    };
    context.RestoreState();
}

Changing the start and end points you can have the gradient paint right to left, up/down, diagonal, etc..

enter image description here

SushiHangover
  • 73,120
  • 10
  • 106
  • 165