The transparency of the UIView shows how sausages are made!

I have a UIView container in which there are two UIImageView inside, one of which partially obscures the other (they are designed to allow random animation of one "layer" or the other.

Sometimes I want to make this container 50% alpha, so users see fade. Here's the problem: setting my container view to 50% alpha makes all my subtitles inheritable and the same, and now you can see through the first subview in the second that in my application it has a strange x-ray effect, which Iโ€™m not looking for.

What I am after, of course, for what the user currently sees, to become 50% transparent is the equivalent of smoothing the visible view into a single bitmap, and then creating that 50% alpha.

What are my best rates for this? Ideally, I would like to avoid actually, dynamically smoothing my views, if I can help, but also the best practices in this greeting. Am I missing something? Since most views have subqueries and will run into this problem, I feel like there is some obvious solution here.

Thanks!

EDIT: Thanks for the thoughts of the people. I just move one image on top of another image, which it only partially hides. And this pair of images must sometimes move together. And sometimes I want it all to disappear, wherever it is, and whatever the state of the pair of images at the moment. Later I want to return it and continue the animation.

Taking a snapshot of the container, either by rendering its layer (?), Or by doing some other off-screen layout on the fly, before alpha from all of this is definitely possible, and I know that there are a couple of ways to do this. But what if the animation should continue while all this is 50% alpha, for example?

There seems to be no obvious solution to what I'm trying to do, which seems strange to me, but thanks to everyone for their contribution.

+4
source share
4 answers

I think smoothing a UIView in a UIImageView is the best bet if you have your heart to provide this feature. In addition, I do not think that smoothing the image will be as difficult as you think. Take a look at the answer provided in this question .

+2
source

I recently had the same issue when I needed to animate layers of content with global transparency. Since my animation was quite complex, I found that smoothing the UIView hierarchy was done for intermittent animation.

The solution I found is to use CALayers instead of UIViews and set the .shouldRasterize property to YES at the container level, so that any sublayers will be automatically flattened before applying opacity.

Here might look like a UIView:

 #import <QuartzCore/QuartzCore.h> //< Needed to use CALayers ... @interface MyView : UIView{ CALayer *layer1; CALayer *layer2; CALayer *compositingLayer; //< Layer where compositing happens. } ... - (void)initialization { UIImage *im1 = [UIImage imageNamed:@"image1.png"]; UIImage *im2 = [UIImage imageNamed:@"image2.png"]; /***** Setup the layers *****/ layer1 = [CALayer layer]; layer1.contents = im1.CGImage; layer1.bounds = CGRectMake(0, 0, im1.size.width, im1.size.height); layer1.position = CGPointMake(100, 100); layer2 = [CALayer layer]; layer2.contents = im2.CGImage; layer2.bounds = CGRectMake(0, 0, im2.size.width, im2.size.height); layer2.position = CGPointMake(300, 300); compositingLayer = [CALayer layer]; compositingLayer.shouldRasterize = YES; //< Here we turn this into a compositing layer. compositingLayer.frame = self.bounds; /***** Create the layer three *****/ [compositingLayer addSublayer:layer1]; //< Add first, so it in back. [compositingLayer addSublayer:layer2]; //< Add second, so it in front. // Don't mess with the UIView layer, it picky; just add sublayers to it. [self.layer addSublayer:compositingLayer]; } - (IBAction)animate:(id)sender { /* Since we're using CALayers, we can use implicit animation * to move and change the opacity. * Layer2 is over Layer1, the compositing is partially transparent. */ layer1.position = CGPointMake(200, 200); layer2.position = CGPointMake(200, 200); compositingLayer.opacity = 0.5; } 
+3
source

Set the bottom UIImageView to .hidden = YES , then set .hidden = NO when you set up the crossfader animation between the top and bottom UIImageViews.

When you need to .alpha = 0.5 all this, you can either set .alpha = 0.5 as a container or as a top image - it does not matter. It might be more efficient to calculate .alpha = 0.5 on the image representation itself, but I donโ€™t know enough about the graphics pipeline on the iPhone to be sure of that.

The only drawback of this approach is that you cannot cross fade when your top image is set to 50% opacity.

0
source

The way to do this is to add ImageViews to the UIWindow (the container will be fake)

-1
source

Source: https://habr.com/ru/post/1305998/


All Articles