Question

I am making a custom UIView subclass that draws a graph (in my own drawRect method) and responds to touch events. I need to have various parts of the graph animate (like blinking hotspots or decelerating a pan after a touch event).

The parameters that drive animation are entirely private to my view, and I would ideally like to keep these behaviors encapsulated in the class. I.e. I don't want the view that hosts this view to have to do anything to make this work. My view knows how often it needs to redraw and I'd like it to be in control of that.

My intuition would be to create a timer in my view class for running my animations, but I'm having trouble getting it to fire and I'm getting an impression that's not the best practice here.

What I need is a way for my view to request that its drawRect get called periodically and ideally be able to control that period dynamically (so I can slow it down when less stuff is happening).

I've looked at some of the CoreAnimation approaches and they look overkill to me, though I'm willing to plunge in to that if it's the right approach.

Is there a simple way for me to get my view's drawRect method to be called on a regular basis? Can I control how often it happens?

Was it helpful?

Solution

you shouldn't call drawRect directly.Instead call setNeedsDisplay , which calls drawRect.

Change few parameters in drawRect and call setneedsdisplay, it will render your view one more time.

But call to drawRect very frequently will impact the performance of your app.Apple even recommends to follow CAAnimations -core animations to do animations not to call drawRect for animation.

OTHER TIPS

Santhu's comment answers my question best.

For very simple animations, I am able to use this mechanism to get my view refreshed periodically:

[self performSelector:@selector(setNeedsDisplay) withObject:nil afterDelay:2.0];

I think for more complex things, I would dive into the CoreAnimation stuff, which I'm sure is less complex than it seems at first blush.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top