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: WT <email@hidden>
- Date: Thu, 2 Apr 2009 02:29:35 +0200
Indeed. I actually already knew that -
performSelector:withObject:afterDelay: executes in the caller's thread
but, in n my haste to throw together a quick test, succumbed to my
thread-safety obsession (thanks to years of Java programming). No
harm, though.
I ended up doing something sligthly different. I collect all the
subviews-to-be in an array, then repeatedly fire a timer that grabs
one view and adds it as a subview to the superview in question.
The issue remains, though, whether this is the right/best approach.
I'm still open to alternative ways and suggestions, if someone would
like to jump in.
( For a refresher on the original question, here's the archive link to
it:
http://lists.apple.com/archives/cocoa-dev/2009/Mar/msg02058.html )
As always, I'm grateful for any responses.
Wagner
On Apr 1, 2009, at 6:59 PM, James Montgomerie wrote:
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