When to call setNeedsDisplay


Just curious as to why put the [self setNeedsDisplay] call in setter, rather than in the touchesBegan:withEvent: ?


You want to call setNeedsDisplay when a change causes the view to need to be redrawn. For example, on a UILabel, the view needs to be redrawn whenever the text changes. So UILabel would need to call [self setNeedsDisplay]; in its -setText method.

To your point, while it could work in the circumstance you list, what if the color changed by some other mechanism? For example, what if you made the color change later on by a shake event? Or if a timer changed the color every 5 seconds or so? The key is, you want to call setNeedsDisplay when a specific change occurs that necessitates a redraw.