2014-04-16 62 views
0

我抓住了我的下一个控制器的视图的屏幕截图(作为UIView对象),并希望将该屏幕截图放置在一个小矩形内我的前任控制者的观点(如预览)。将较大的UIView对象放在较小的UIView对象中的最佳方式是什么?最好的方法来放大一个较大的UIView较小的一个缩放较大的一个适合

这不起作用:

UIView *screenshot = .... // screenshot from the next controller's view 
smallViewBox.contentMode = UIViewContentModeScaleAspectFit; 
[smallViewBox addSubView:screenshot]; 

回答

1

尝试设置较大视图的边界以匹配较小视图的边界。我刚掀起了一个简单的例子:

UIView *largeView = [[UIView alloc] initWithFrame:CGRectMake(40, 40, 60, 60)]; 
largeView.backgroundColor = [UIColor redColor]; 
[self.view addSubview:largeView]; 

UIView *smallView = [[UIView alloc] initWithFrame:CGRectMake(50,50,40,40)]; 
smallView.backgroundColor = [UIColor greenColor]; 
[self.view addSubview:smallView]; 

largeView.bounds = smallView.bounds; 

如果您注释掉largeView.bounds = smallView.bounds绿色(小)框将是唯一一个可见的,因为它正在草拟了在红盒子控制器的视图(在这种情况下,两个视图是兄弟姐妹)。为了使大图中较小的一个的子视图,并将其限制在较小的区域,你可以这样做:

UIView *largeView = [[UIView alloc] initWithFrame:CGRectMake(40, 40, 60, 60)]; 
largeView.backgroundColor = [UIColor redColor]; 

UIView *smallView = [[UIView alloc] initWithFrame:CGRectMake(50,50,40,40)]; 
smallView.backgroundColor = [UIColor greenColor]; 
[self.view addSubview:smallView]; 

largeView.frame = CGRectMake(0, 0, smallView.bounds.size.width, smallView.bounds.size.height); 
[smallView addSubview:largeView]; 

这将导致更大的视图的红色可见 - 包括绿色小视图的背景。在这种情况下,大视野是小视野的一个孩子,占据了整个地区。

+0

感谢您的详细解答 – Nihat

1

您可以设置就可以了尺度变换。

screenshot.transform = CGAffineTransformMakeScale(0.5, 0.5); 
+0

Karah,你的回答是正确的,因为它缩放UIView,但是,当我将它添加到较小的视图时,它出现在下面的某个地方。我想我需要一些小装备才能让你的版本工作。 GWhite的回答完美无瑕,所以我接受了他的回答,但给了你一个正确的答案。谢谢 – Nihat

+0

赞赏。我同意GWhite的回答更准确。使用缩放转换将需要您根据UIView进行一些数学运算。 – joels

+0

实际上,我使用变换工作,我在保持宽高比的同时精确缩放了它。我只需要将截图的来源更改为(0,0)。所以,你的作品也是如此。再次感谢 – Nihat

相关问题