1

I'm developing a iOS project that involves drawing small graphics (lines and paths) on the screen.

I initially chose to use Quartz instead of OpenGL, because I need to display some basic shapes and I need to update them every 5 seconds, so I thought Quartz was better and easier.

I found out that I can't simply draw in a view, but I have to subclass a UIView and draw in the drawRect method.

In my project, the user should be able to pinch and zoom on graphics, so I planned to add a pinchgesture to the view, but I am doubtful about how to redraw everything after the pinch. Do I have to erase everything and re-add the subviews so the drawRect will trigger or is there a better way to do this?

Thanks a lot.

Nathaniel Waisbrot
  • 23,261
  • 7
  • 71
  • 99
GavynSykes
  • 133
  • 2
  • 15
  • not a possible answer but check this http://blog.teamtreehouse.com/creating-a-fun-iphone-app-called-bearded-part-1 – Joseph Feb 05 '13 at 13:04

1 Answers1

1

When using Quartz, you technically don't have to subclass the view and replace the drawRect, but it probably is best practice. When you want to redraw your window, just call [self setNeedsDisplay]; (if calling from the subclassed view, or [self.view setNeedsDisplay]; if doing it from the view controller). This will result in calling your drawRect method for you and it takes care of everything for you.

See the setNeedsDisplay documentation for more information.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • so You mean that for example I change my drawing variables to new values and then trigger setNeedsDisplay, right? – GavynSykes Feb 05 '13 at 13:26
  • @GavynSykes - Yep. See the second code example in http://stackoverflow.com/a/14203167/1271826 for an example of combining a gesture and a `setNeedsDisplay`. Note, this is doing something slightly different than you are (this is animating the drawing of a line from the start of a gesture to the end of a gesture), but it illustrates (a) the integration of gestures and Quartz; (b) right time to call `setNeedsDisplay` in `touchesMoved` and `touchesEnded`; and (c) shows you the difference between doing it in your view controller or using a subclassed `UIView`. – Rob Feb 05 '13 at 13:29