Create Designable Subclass of UIView To Create Gradient Layer on Draw View

Source Code

// MARK: – Designable GradientView Class
@IBDesignable final class GradientView: UIView {

@IBInspectable var startColor: UIColor = UIColor.init(colorLiteralRed: 158.0/255.0, green: 201.0/255.0, blue: 250.0/255.0, alpha: 1.0)
@IBInspectable var endColor: UIColor = UIColor.white

/* Initiate the view with Radial Gradient filled with defined colors */
override func draw(_ rect: CGRect) {

// Setup view
let colors = [endColor.cgColor, startColor.cgColor] as CFArray
let locations = [ 0.0, 1.0 ] as [CGFloat]
let radius = min((self.bounds.size.height + 50), (self.bounds.size.width + 50))
let center = CGPoint.init(x: self.bounds.size.width / 2, y: self.bounds.size.height / 2)

// Prepare a context and create a color space
let context = UIGraphicsGetCurrentContext()
context!.saveGState()
let colorSpace = CGColorSpaceCreateDeviceRGB()

// Create gradient object from our color space, color components and locations
let gradient = CGGradient.init(colorsSpace: colorSpace, colors: colors, locations: locations)

// Draw a gradient
context!.drawRadialGradient(gradient!, startCenter: center, startRadius: 0.0, endCenter: center, endRadius: radius, options: CGGradientDrawingOptions(rawValue: 0))
context?.restoreGState()
}

}

Use

Create Designable Subclass of UIView To Create Gradient Layer on Draw View

Add Circle CALayer Inside A View Using Core Graphics iOS (Xamarin.iOS & Objective-C)

Xamarin.iOS

public CALayer createImage(UIColor fillerColor, UIColor borderColor, UIView view, nfloat x, nfloat y)
        {
            // Get the size
            CGSize canvasSize = view.Frame.Size;
            nfloat scale = UIScreen.MainScreen.Scale;

            // Resize for retina with the scale factor
            canvasSize.Width *= scale;
            canvasSize.Height *= scale;

            // Create the context

            UIGraphics.BeginImageContext(canvasSize);
            CGContext ctx = UIGraphics.GetCurrentContext();

            // setup drawing attributes
            ctx.SetLineWidth(0.5f * scale);
            ctx.SetStrokeColor(borderColor.CGColor);
            ctx.SetFillColor(fillerColor.CGColor);

            // setup the circle size
            CGRect circleRect = new CGRect(0, 0, canvasSize.Width, canvasSize.Height);
            circleRect = circleRect.Inset(0.5f,0.5f);

            // Draw the Circle
            ctx.FillEllipseInRect(circleRect);
            ctx.StrokeEllipseInRect(circleRect);

            // Create Image
            UIImage retImage = UIGraphics.GetImageFromCurrentImageContext();

            CALayer aLayer = new CALayer();
            CGRect startFrame = new CGRect(x, y, view.Frame.Width  (2*x), view.Frame.Height  (2 * y));
            aLayer.Contents = retImage.CGImage;
            aLayer.Frame = startFrame;

            return aLayer;
        }

USE: createImage(UIColor.White, UIColor.Black, cell.ImgvEndCircle, 3f, 3f)

Objective-C

-(CALayer *)createImage:(UIColor *)fillerColor withbordercolor: (UIColor *)borderColor withView:(UIView*)view xPosition:(CGFloat)x yPosition:(CGFloat)y{

// Get the size
CGSize canvasSize = view.frame.size;
CGFloat scale = [UIScreen mainScreen].scale;

// Resize for retina with the scale factor
canvasSize.width *= scale;
canvasSize.height *= scale;

// Create the context
UIGraphicsBeginImageContext(canvasSize);
CGContextRef ctx = UIGraphicsGetCurrentContext();

// setup drawing attributes
CGContextSetLineWidth(ctx, 0.5 * scale);
CGContextSetStrokeColorWithColor(ctx, borderColor.CGColor);
CGContextSetFillColorWithColor(ctx, fillerColor.CGColor);

// setup the circle size
CGRect circleRect = CGRectMake( 0, 0, canvasSize.width, canvasSize.height );
circleRect = CGRectInset(circleRect, 0.5, 0.5);

// Draw the Circle
CGContextFillEllipseInRect(ctx, circleRect);
CGContextStrokeEllipseInRect(ctx, circleRect);

// Create Image
UIImage *retImage = UIGraphicsGetImageFromCurrentImageContext();

CALayer *aLayer = [CALayer layer];
CGRect startFrame = CGRectMake(x, y, view.frame.size.width-(2*x), view.frame.size.height-(2*y));
aLayer.contents = (id)retImage.CGImage;
aLayer.frame = startFrame;
return aLayer;
}

Add Circle CALayer Inside A View Using Core Graphics iOS (Xamarin.iOS & Objective-C)