gradient - NSGradient drawInBezierPath iOS equivalent -
is there ios equivalent - (void)drawinbezierpath:(nsbezierpath *)path angle:(cgfloat)angle
?
i need draw gradient inside uibezierpath , cannot work. gradient draws on screen.
this example creates gradient inside uibezierpath in swift 3. draws slanting line green/white gradient.
import uikit class drawingview: uiview { override init(frame: cgrect) { super.init(frame: frame) self.isopaque = false } required init?(coder adecoder: nscoder) { super.init(coder: adecoder) } override func draw(_ rect: cgrect) { let startpoint = cgpoint(x:100, y:100) let endpoint = cgpoint(x: 300, y:400) let context = uigraphicsgetcurrentcontext()! context.setstrokecolor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0); // create line context.move(to: startpoint) context.addline(to: endpoint) context.setlinewidth(4) // use line created above clipping mask context.replacepathwithstrokedpath() context.clip() // create gradient let locations: [cgfloat] = [ 0.0, 0.5 ] let colors = [uicolor.green.cgcolor, uicolor.white.cgcolor] let colorspace = cgcolorspacecreatedevicergb() let gradient = cggradient(colorsspace: colorspace, colors: colors cfarray, locations: locations) let gradientstartpoint = cgpoint(x: rect.midx, y: rect.miny) let gradientendpoint = cgpoint(x: rect.midx, y: rect.maxy) context.drawlineargradient(gradient!, start: gradientstartpoint, end: gradientendpoint, options: .drawsbeforestartlocation) uigraphicsendimagecontext() } }
Comments
Post a Comment