Hello.
I subclassed a UIView to do some drawing. Inside the view, I created a frame that takes only a part of the view.
override func drawRect(rect: CGRect)
{
self.rectangleFrame = CGRectMake(0,20,380,580)
...
}
At the moment, the frame is filled with blue color:
let frameBezierPath = UIBezierPath(roundedRect: self.rectangleFrame, cornerRadius: 5)
UIColor.blueColor().setFill()
frameBezierPath.fill()
Instead, I want to fill the frame with gradient color. Usually, the way I do it is using a context:
let startColor = UIColor(red: 30/255, green: 65/255, blue: 255/255, alpha: 1.0)
let endColor = UIColor(red: 204/255, green: 255/255, blue: 255/255, alpha: 1.0)
let locations : [CGFloat] = [0, 1.0]
let colorspace = CGColorSpaceCreateDeviceRGB()
let colors = [startColor.CGColor, endColor.CGColor]
let gradient = CGGradientCreateWithColors(colorspace, colors, locations)
let startPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMinY(rect));
let endPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMaxY(rect));
CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, CGGradientDrawingOptions(rawValue: 0))
CGContextSaveGState(context);
if (!CGContextIsPathEmpty(context))
{
CGContextClip(context);
}
CGContextRestoreGState(context);
However, in my case I cannot do, since it fills the whole UIView with the gradient color, and I just want to limit it to the frame only. I am a bit confused how to use setFill() with gradient.
Thank you!