Re: How to animate the drawing of UIImages inside a drawRect: method of a UIView?
Re: How to animate the drawing of UIImages inside a drawRect: method of a UIView?
- Subject: Re: How to animate the drawing of UIImages inside a drawRect: method of a UIView?
- From: James Montgomerie <email@hidden>
- Date: Fri, 3 Apr 2009 14:14:33 +0100
On 2 Apr 2009, at 22:33, WT wrote:
25 was just a test. The actual app has 2 interlocking 7x7 grids, so
the number of image views is actually 98. I should explain that this
animated laying out of image views happens only once and, from that
point on, the image views all remain in the same place, though each
image itself is animated. Here's a screen shot to clarify things:
<grid.png>
Presuming that this is full-size, It is uncommon to have views that
are so small (the balls must be, what, 4X4 pixels or something?), I'd
say. Intuitively, it 'looks' to me like the 'normal' architecture for
something like this would be to have one custom view for the entire
grid (of course, there are always exceptions, and I don't know the
full background of your app).
The balls pulsate, growing and shrinking in size, an effect that I
accomplish using UIImageView's -startAnimating, but the animation I
was referring to in my previous posts has to do with adding each
image view to its proper place.
My question boils down to this: if I am going to use -startAnimating
for the pulsation, then each ball *has* to be an instance of
UIImageView, which requires me to add 98 subviews to the superview
and the laying out animation is as I described in the 25-image
example. On the other hand, I could forgo using 98 subviews and do
everything (one-time animated layout *and* animated pulsation)
inside the superview's -drawRect. This second option is where I'm
unsure how to proceed.
The solution I have now (98 subviews) works and is convenient but is
taxing the device, so I'm looking for a better alternative,
performance-wise.
If it's taxing the device, then I would recommend creating your own
view, and compositing the images yourself in drawRect:, as you suggest.
To get your animation, just set a timer at your frame rate and call
setNeedsDisplay on the view in its callback. Inside drawRect, you
could either work out what to display based on the time, or you could
set some flags in your timer routine setting 'state' for drawrect to
use before you make the setNeedsDisplay call.
Look at the UIImage's drawInRect and/or drawAtPoint: methods for how
to draw the images. There are methods that let you specify alpha and
blend mode, and the drawInRect method will scale the image to fit the
rect you specify.
Note that you won't need an UIImage instance for every 'ball' - ones
that are the same looking can just be drawn by reusing the same
UIImage instance and drawing it multiple times.
If this is still taxing, you might want to look into using a custom
OpenGL view to do your rendering (although you might want to run
through a few OpenGL tutorials first if you've never used OpenGL
before, I'd say it's quite a different style of API in many ways).
Jamie.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden