small medium large xlarge

Generic-user-small
14 Jun 2009, 22:37
Bryan Hughes (8 posts)

I have hopefully a simple question but being very new to the iPhone and Core Animation I am finding this issue annoying and frustrating that I can’t figure it out.

Basically, I have custom view that extends UIView which has several UIImageViews applied in the proper order to the UIViews layer. The UIImageViews are managing the animations of an UIImage array. When I apply a transform to the image views layer, I get the expected results except that the layer appears now to be on top of the first layer that I applied to the views layer.

Here is the code:

- (void)setDirection:(int)theDirection
{
	direction = theDirection;
	if ( theDirection == kRightFacing ) {
		kittenWalkingImageView.layer.transform = CATransform3DMakeRotation(M_PI, 0.0f, 1.0f, 0.0f);
	}
	else {
		kittenWalkingImageView.layer.transform = originalTransform;
	}

	[self.layer insertSublayer:kittenWalkingImageView.layer above:kittenShadowImageView.layer];
	[self setNeedsLayout];
	
	NSLog( @"set direction to %d\n", direction );
}

When the animation loop runs, and the view is left facing, the image views are rendered correctly from the bottom up. When I apply the transform to be right facing, the shadow, which was added first to superview is now on top. When I switch the view during the animation loop so that it is back to left face, it renders in the correct order and the shadow is once again on bottom.

Any ideas or pointers or direction I can look to will be greatly appreciated!!!

Thanks Bryan

Biopic_100x100_pragsmall
15 Jun 2009, 12:07
Bill Dudney (917 posts)

Hi Bryan,

Well the biggest issue here is that you are manipulating the layer that is ‘owned’ by your view. That is almost always a bad idea.

I would instead make a layer subclass that can animated an array of images and then add them as sublayers to a container layer you add to the view.

I’m not 100% sure this will fix the shadow problem but it seems likely.

Good luck and let us know what you find.

Generic-user-small
15 Jun 2009, 16:19
Bryan Hughes (8 posts)

Bill, thanks for the tip. I will implement it tonight and get back to you. Also I wanted to thank you for your book as well as for your attention to this forum. I have been a programmer (C and Java) for over 20 years now but never have I done animation or game programming. For a noob, having an expert like yourself to answer sometimes stupid questions is really a great thing.

The reason why I went down this path was because it worked. I originally was trying to apply the transform of the parent UIView’s superlayer but all I succeeded in doing was making the views image disappear. I am sure there is a good reason but after spending a while trying to figure it out, I decided to transform the contained layers directly and that work nicely except for this shadow issue.

Thanks again!!!

You must be logged in to comment