Search code examples
iosuiviewcore-graphicstouchuitouch

Make touches leave glowing trails in UIView subclass


In a UIView subclass, what's the best way to have touches leave glowing trails that fade after a short time?

'Best' here mainly means simplest and most efficient. I'm intercepting touch events with touchesBegan etc. I would love some specific details of how to use Core Graphics or other strategies to accomplish this. The fading is probably the key here.

EDIT: I have experience drawing paths based on touches using Core Graphics in various ways in the drawRect method. I'm curious about particular ways of implementing the fading mechanism that will not have a huge performance cost.


Solution

  • I'd look into CAEmitterLayer. It's highly-optimized, but it's only available on iOS 5. To create the glow trail, look into tweaking the lifetime property of the emitter.

    To use the emitter to accomplish your goal, I'd just say you should implement some sort of tracking in your -touchesMoved: withEvent: that moves particle emitter along with the user's location.