Search code examples
ioscocoa-touchdrawingtouchdrawrect

iOS: Standard rules to follow when implementing drawRect: with draggable objects


My app allows the user to drag circles around on a screen. I'm wondering whether there are any standard steps to take in order to implement this, especially in regards to marking the correct areas of the view as dirty. I'm currently doing the following:

//Get initial touch point and draw a circle there. Use touch point and size of circle to pass the mark the appropriate CGRect as dirty so that it gets redrawn
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

//Get new touchpoint and draw a circle there. Use the union of the previous CGRect and the new one (based on the touch point and circle size) to tell drawRect: what area of the screen to redraw.
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

Does this seem right, or is there some other fundamental piece that I should be doing?


Solution

  • Assuming that your view is what is drawing the circles, then what you're doing should be fine. Just use [self setNeedsDisplayInRect: circleRect] to mark the previous and new positions of the circle as dirty.