views:

236

answers:

3

I have a UIView container that has two UIImageViews inside it, one partially obscuring the other (they're being composed like this to allow for occasional animation of one "layer" or another.

Sometimes I want to make this container 50% alpha, so what the users sees fades. Here's the problem: setting my container view to 50% alpha makes all my subviews inherit this as well, and now you can see through the first subview into the second, which in my application has a weird X-Ray effect that I'm not looking for.

What I'm after, of course, is for what the user currently sees to become 50% transparent-- the equivalent of flattening the visible view into one bitmap, and then making that 50% alpha.

What are my best bets for accomplishing this? Ideally would like to avoid actually, dynamically flattening the views if I can help it, but best practices on that welcome as well. Am I missing something obvious? Since most views have subviews and would run into this issue, I feel like there's some obvious solution here.

Thanks!

EDIT: Thanks for the thoughts folks. I'm just moving one image around on top of another image, which it only partially obscures. And this pair of images has to move together sometimes, as well. And sometimes I want to fade the whole thing out, wherever it is, and whatever the state of the image pair is at the moment. Later, I want to bring it back and continue animating it.

Taking a snapshot of the container, either by rendering its layer (?) or by doing some other offscreen compositing on the fly before alpha'ing out the whole thing, is definitely possible, and I know there are a couple ways to do it. But what if the animation should continue to happen while the whole thing's at 50% alpha, for example?

It sounds like there's no obvious solution to what I'm trying to do, which seems odd to me, but thank you all for the input.

A: 

A way to do this would be to add the ImageViews to the UIWindow (the container would be a fake one)

Julien
Can you elaborate on this? What would adding them to the window do to help? Thanks.
quixoto
I think Julien is misunderstanding your question. You aren't trying to only fade the UIView. You are trying to fade the UIView AND only the portions of the UIImageViews that are visible to the user. Adding the UIImageViews to the UIWindow will not help with this.
glorifiedHacker
Yep i misunderstood, sorry
Julien
A: 

Set the bottom UIImageView to have .hidden = YES, then set .hidden = NO when you setup a cross-fade animation between the top and bottom UIImageViews.

When you need to fade the whole thing, you can either set .alpha = 0.5 on the container view or the top image view - it shouldn't matter. It may be computationally more efficient to set .alpha = 0.5 on the image view itself, but I don't know enough about the graphics pipeline on the iPhone to be sure about that.

The only downside to this approach is that you can't do a cross-fade when your top image is set to 50% opacity.

Nick Forge
Because the UIImageViews only partially overlap, I don't think this will accomplish what quixoto is looking for. The bottom UIImageView is partly sticking out, so making it hidden probably isn't an option.
glorifiedHacker
You're right, I missed the 'partially obscured'. I guess we need more info on exactly what quixoto is trying to achieve to be able to answer definitively.
Nick Forge
+2  A: 

I think that flattening the UIView into a UIImageView is your best bet if you have your heart set on providing this feature. Also, I don't think that flattening the image is going to be as complicated as you might think. Task a look at the answer provided in this question.

glorifiedHacker