Mirroring UIView

2019-08-10 19:17发布

问题:

I have a UIView with a transparent background, and some buttons. I would like to capture the drawing of the view, shrink it, and redraw (mirror) it elsewhere on the screen. (On top of another view.) The buttons can change, so it isn't static.

What would be the best way to do this?

回答1:

Check a nice sample code http://saveme-dot-txt.blogspot.com/2011/06/dynamic-view-reflection-using.html following WWDC sessions. It uses

CAReplicatorLayer

for reflection, pretty easy to implement and looks really smooth and impressive.



回答2:

The general idea will be to get a UIView's layer to draw itself into a context and then grab a UIImage out of it.

UIGraphicsBeginImageContext(view.frame.size);

[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

You will also need to #import <QuartzCore/QuartzCore.h>



回答3:

If you don't really need to capture the drawing (from what you describe, it seems unlikely that you need an image), create another instance of the view and apply a transform. Something like...

UIView* original [UIView alloc] initWithFrame:originalFrame];
UIView* copy = [[UIView alloc] initWithFrame:copyFrame];

// Scale down 50% and rotate 45 degrees
//
CGAffineTransform t = CGAffineTransformMakeScale(0.5, 0.5);
copy.transform = CGAffineTransformRotate(t, M_PI_4);

[someSuperView addSubview:original];
[someSuperView addSubview:copy];

// release, etc.

I added the rotation just to show that you can do a variety of different things with transformations.