ios - UIBezierPath Rotation around a UIView's center -
i'm creating custom uiview
, in implement draw(rect:)
method drawing circle large width using uibezierpath, draw square on top (as shown in picture, don't consider colors or size). try creating rotated copies of square, match "settings" icon (picture 2, consider outer ring). last thing, need rotate square using cgaffinetransform(rotationangle:)
problem rotation's center origin of frame, , not center of circle. how can create rotation around point in view?
as demonstration of @duncanc's answer (up voted), here drawing of gear using cgaffinetransform
s rotate gear tooth around center of circle:
class gear: uiview { var linewidth: cgfloat = 16 let boxwidth: cgfloat = 20 let toothangle: cgfloat = 45 override func draw(_ rect: cgrect) { let radius = (min(bounds.width, bounds.height) - linewidth) / 4.0 var path = uibezierpath() path.linewidth = linewidth uicolor.white.set() // draw circle path.move(to: cgpoint(x: center.x + radius, y: center.y)) path.addarc(withcenter: cgpoint(x: center.x, y: center.y), radius: radius, startangle: 0, endangle: 2 * .pi, clockwise: true) path.stroke() // box gear tooth path = uibezierpath() let point = cgpoint(x: center.x - boxwidth / 2.0, y: center.y - radius) path.move(to: point) path.addline(to: cgpoint(x: point.x, y: point.y - boxwidth)) path.addline(to: cgpoint(x: point.x + boxwidth, y: point.y - boxwidth)) path.addline(to: cgpoint(x: point.x + boxwidth, y: point.y)) path.close() uicolor.red.set() // draw tooth every toothangle degrees _ in stride(from: toothangle, through: 360, by: toothangle) { // move origin center of circle path.apply(cgaffinetransform(translationx: -center.x, y: -center.y)) // rotate path.apply(cgaffinetransform(rotationangle: toothangle * .pi / 180)) // move origin original location path.apply(cgaffinetransform(translationx: center.x, y: center.y)) // draw tooth path.fill() } } } let view = gear(frame: cgrect(x: 0, y: 0, width: 200, height: 200))
here running in playground:
Comments
Post a Comment