i want draw jigsaw shape using cashapelayer
. know how draw square dont know how draw half circle (curves).
i've coded example using swift , playground.
import uikit let width : cgfloat = 400 let height : cgfloat = 400 let margin : cgfloat = 50 let topleftcorner = cgpoint(x: 0 + margin, y: 0 + margin) let topmidle = cgpoint(x: width/2.0, y: 0 + margin) let toprightcorner = cgpoint(x: width - margin, y: 0 + margin) let rightmidle = cgpoint(x: width - margin, y: height/2.0) let bottomrightcorner = cgpoint(x: width - margin, y: height - margin) let bottommidle = cgpoint(x: width/2.0, y: height - margin) let bottomleftcorner = cgpoint(x: 0 + margin, y: height - margin) let leftmidle = cgpoint(x: 0 + margin, y: height/2.0) let jigsawpiece = uiview(frame: cgrect(x: 0, y: 0, width: width, height: height)) let path = uibezierpath() // starting point path.move(to: topleftcorner) // draw top circle path.addarc(withcenter: topmidle, radius: margin, startangle: 0, endangle: cgfloat(m_pi), clockwise: false) // draw line corner path.addline(to: toprightcorner) // draw right circle path.addarc(withcenter: rightmidle, radius: margin, startangle: cgfloat(m_pi/2.0), endangle: cgfloat(3*m_pi/2.0), clockwise: false) // draw line corner path.addline(to: bottomrightcorner) // draw bottom cut path.addarc(withcenter: bottommidle, radius: margin, startangle: 0, endangle: cgfloat(m_pi), clockwise: false) // draw line path.addline(to: bottomleftcorner) // draw left cut path.addarc(withcenter: leftmidle, radius: margin, startangle: cgfloat(m_pi/2.0), endangle: cgfloat(3*m_pi/2.0), clockwise: false) // draw line path.addline(to: topleftcorner) path.close() let layer = cashapelayer() layer.path = path.cgpath layer.fillcolor = uicolor.red.cgcolor jigsawpiece.layer .addsublayer(layer) jigsawpiece
this output. can convert objective c if necessary.
Comments
Post a Comment