ios 如何从 CGPoint 和 CGSize 创建 CGRect?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12063650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I create a CGRect from a CGPoint and CGSize?
提问by inorganik
I need to create a frame for a UIImageView
from a varying collection of CGSize
and CGPoint
, both values will always be different depending on user's choices. So how can I make a CGRect
form a CGPoint
and a CGSize
? Thank you in advance.
我需要UIImageView
从不同的CGSize
and集合中为 a 创建一个框架,CGPoint
根据用户的选择,这两个值总是不同的。所以,我怎样才能使一个CGRect
形式CGPoint
和CGSize
?先感谢您。
回答by Jim
Two different options for Objective-C:
Objective-C 的两个不同选项:
CGRect aRect = CGRectMake(aPoint.x, aPoint.y, aSize.width, aSize.height);
CGRect aRect = { aPoint, aSize };
Swift 3:
斯威夫特 3:
let aRect = CGRect(origin: aPoint, size: aSize)
回答by coco
Building on the most excellent answer from @Jim, one can also construct a CGPoint and a CGSize using this method. So these are also valid ways to make a CGRect:
基于@Jim 的最佳答案,您还可以使用此方法构建 CGPoint 和 CGSize。因此,这些也都是有效的方法来进行的CGRect:
CGRect aRect = { {aPoint.x, aPoint.y}, aSize };
CGrect aRect = { aPoint, {aSize.width, aSize.height} };
CGRect aRect = { {aPoint.x, aPoint.y}, {aSize.width, aSize.height} };
回答by rooster117
CGRectMake(yourPoint.x, yourPoint.y, yourSize.width, yourSize.height);
回答by Nikolay Shubenkov
you can use some sugar syntax. For example:
你可以使用一些糖语法。例如:
This is something like construction block you can use for more readable code:
这类似于构造块,您可以将其用于更具可读性的代码:
CGRect rect = ({
CGRect customCreationRect
//make some calculations for each dimention
customCreationRect.origin.x = CGRectGetMidX(yourFrame);
customCreationRect.origin.y = CGRectGetMaxY(someOtherFrame);
customCreationRect.size.width = CGRectGetHeight(yetAnotherFrame);
customCreationRect.size.height = 400;
//By just me some variable in the end this line will
//be assigned to the rect va
customCreationRect;
)}