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: Wed, 1 Apr 2009 17:59:07 +0100
This is not doing exactly what you think it is.
You are certainly right that UIKit calls should /always/ be made from
the main thread, but the "performSelector:withObject:AfterDelay"
method actually performs the selector on the same thread that you call
it from, not a separate thread. This means that - presuming
viewDidLoad: is getting called on the main thread, which it is if it's
the system that's calling it - your "addViewInSeparateThread:" is in
fact getting called on the main thread.
You could not have the addViewInSeparateThread: method at-all, and
just call performSelector:withObject:AfterDelay with your
@selector(addViewInMainThread:) selector, and it would have the same
effect.
Jamie.
On 31 Mar 2009, at 17:20, WT wrote:
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.
Wagner
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 (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
_______________________________________________
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