I am writing a Cocoa application for OS X, where the user can draw squares on an NSView instance by clicking with the mouse. Currently I am making the squares disappear after 2 seconds, using the performSelector:withObject:afterDelay: method of NSObject, to force a redraw of the view, with no square included.
However, instead of just disappearing, I would like the squares to fade out gradually. I’ve tried using an NSTimer to periodically force a redraw, with the opacity of the square decreasing to 0 over 2 seconds, but this seems rather inelegant and probably inefficient, especially if I have a lot of squares.
Is there an idiomatic way to do this?
UPDATE: just to clarify, I want each square drawn in the view to have an independent fade starting from the point at which it’s drawn, I’m not looking to fade out the entire view.
The solution I’ve ended up using is to create a
CALayerinstance for each square, rather than usingNSRectFillto draw the squares. The opacity of eachCALayerinstance can then be independently animated using aCABasicAnimationinstance. E.g.