0

Hi everyone I need to draw with CGContext a circle like this ..

enter image description here

How is this possible?

Should I use an image within CGContext or is it possible to use only ctx.fillEllipse(in: rect)?

For now I have made a simple white circle

private func drawCursor(inContext ctx: CGContext) {
   ctx.saveGState()

   let center = pointFrom(angle: currentAngle)
   let rect = CGRect(x: center.x, y: center.y, width: cursorSize, height: cursorSize)

   cursorColor.setFill()

   ctx.setShadow(offset: CGSize(width: 0, height: 0), blur: 3, color: cursorShadowColor.cgColor)

   ctx.fillEllipse(in: rect)
   ctx.restoreGState()
}

Thank you all for the support

kAiN
  • 2,559
  • 1
  • 26
  • 54
  • Which part are you having trouble with? This is a trivial task, unless perhaps I’m missing what “a circle like this” means. To me it looks like a stroked circle and a smaller filled circle; am I wrong about that? Or is the shadow the problem? – matt Apr 19 '20 at 12:08
  • why are you using CGConntext? Where are you use this object? – Dilan Apr 19 '20 at 12:08
  • @DilanAnuruddha UIControl class – kAiN Apr 19 '20 at 12:09
  • @matt In a nutshell I would need to draw a circle with red border and white fill and inside this circle I have to draw another one with red fill ... I wanted to know which way I have to follow to get this with `CGContext` in `drawRect` func – kAiN Apr 19 '20 at 12:11
  • In which drawRect? If this is a UIView’s drawRect you are in a graphics context already; just draw. – matt Apr 19 '20 at 12:13
  • “In a nutshell I would need to draw a circle with red border and white fill and inside this circle I have to draw another one with red fill” Correct, and that’s trivial so I don’t understand what the question is about. – matt Apr 19 '20 at 12:16
  • @matt I wanted to know if I have to repeat the drawing of a circle twice or if there were other ways to achieve my goal.. get a new CGContext? – kAiN Apr 19 '20 at 12:17
  • You don’t need a new anything. A context has states. Set a red stroke and white fill and draw the outer circle and stroke/fill it. Now set a red fill and draw the inner circle and fill it. Just like any drawing program. – matt Apr 19 '20 at 12:21
  • @matt Perfect Matt thank you very much ... Can I ask you a favor? could you show me your logic using the example code I entered? For me to understand well – kAiN Apr 19 '20 at 12:22
  • My logic is just that that’s how drawing works. See my http://www.apeth.com/iOSBook/ch15.html#_graphics_context_settings (old but the idea remains exactly the same). – matt Apr 19 '20 at 12:29
  • @matt yes yes .. I didn't want to criticize your comment :) I just wanted to understand the code implementation well .. With the examples I understand better :) .. Sorry if I seemed annoyed, it's not true :) – kAiN Apr 19 '20 at 12:31
  • I do not propose to write your code _for_ you. There is more than enough drawing examples on Stack Overflow. My book examples, up to date, are at https://github.com/mattneub/Programming-iOS-Book-Examples. Drawing is book 2 chapter 2. – matt Apr 19 '20 at 12:34

0 Answers0