Mirroring UIView

雨燕双飞 提交于 2019-12-24 09:59:10

问题


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.



来源:https://stackoverflow.com/questions/8675867/mirroring-uiview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!