Mirroring UIView

2019-08-10 19:14发布

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?

3条回答
地球回转人心会变
2楼-- · 2019-08-10 19:38

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楼-- · 2019-08-10 19:39

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.

查看更多
The star\"
4楼-- · 2019-08-10 19:53

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.

查看更多
登录 后发表回答