Subject: How to animate the drawing of UIImages inside a
drawRect: method of a UIView?

Hello again,

in anticipation that the answer to my question might be to add UIImageViews as subviews rather than to draw UIImages from within the superview's drawRect:, I tried the following little test, which works like a charm. The question remains, though, whether this is the right/ best approach.


The following code goes in the view controller managing the view where the images should be drawn into. It adds 25 image views to the view managed by the view controller, as a 5 x 5 grid, with a separation of 40 pixels between images. The grid rectangle has an origin at x = 30, y = 50. The time delay between images is 0.1 second, for a total animation time of 2.5 seconds.

- (void) viewDidLoad
for (int i = 0; i < 5; ++i)
CGFloat x = 40*i + 30;
for (int j = 0; j < 5; ++j)
CGFloat y = 40*j + 50;

UIImageView* imgView = [[UIImageView alloc]
initWithImage: [UIImage imageNamed: @"img.png"]];

CGRect frame = imgView.frame;
frame.origin.x = x - frame.size.width / 2.0;
frame.origin.y = y - frame.size.height / 2.0;
imgView.frame = frame;

[self performSelector: @selector(addViewInSeparateThread:)
withObject: imgView
afterDelay: (5*j + i) / 10.0];

[imgView release];

- (void) addViewInSeparateThread: (UIView*) imgView
[self performSelectorOnMainThread: @selector(addViewInMainThread:)
withObject: imgView
waitUntilDone: YES];

- (void) addViewInMainThread: (UIView*) imgView
[self.view addSubview: imgView];


Cocoa-dev mailing list (Cocoa-dev@xxxxxxxxxxxxxxx)

Please d...

o not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)

Help/Unsubscribe/Update your Subscription:

This email sent to maillists@xxxxxxxxx