Skip to content

Instantly share code, notes, and snippets.

@baileysh9
Last active July 14, 2017 17:54
Show Gist options
  • Save baileysh9/f6193d0a1a1fba06cad8a53bafaba216 to your computer and use it in GitHub Desktop.
Save baileysh9/f6193d0a1a1fba06cad8a53bafaba216 to your computer and use it in GitHub Desktop.
iOS Implementation of Gradient View Render
public void CreateGradient()
{
CAGradientLayer gradient = new CAGradientLayer();
CGRect tempRect = new CGRect(0, 0, this.viewWidth, this.viewHeight);
gradient.Frame = tempRect;
//Need to convert the colors to CGColor objects
CGColor[] cgColors = new CGColor[gradientColors.Count()];
for (int i = 0; i < gradientColors.Count(); i++)
{
Color temp = gradientColors[i];
cgColors[i] = temp.ToCGColor();
}
gradient.Colors = cgColors;
//Determine if the gradient should be vertical or left to right
if (leftToRight)
{
gradient.StartPoint = new CGPoint(0, 0.5);
gradient.EndPoint = new CGPoint(1, 0.5);
}
else
{
gradient.StartPoint = new CGPoint(0.5, 0);
gradient.EndPoint = new CGPoint(0.5, 1);
}
CGRect rec = new CGRect(0, 0, viewWidth, viewHeight);
var rounded = UIRectCorner.TopLeft | UIRectCorner.TopRight | UIRectCorner.BottomLeft | UIRectCorner.BottomRight;
if (roundCorners)
{
UIBezierPath path = UIBezierPath.FromRoundedRect(rec, rounded, new CGSize(cornerRadius, cornerRadius));
CAShapeLayer shape = new CAShapeLayer();
shape.Path = path.CGPath;
gradientView.Layer.Mask = shape;
}
gradientView.Layer.InsertSublayer(gradient, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment