如何从CGPoint和CGSize创buildCGRect?

我需要从CGSizeCGPoint的不同集合中为UIImageView创build一个框架,根据用户的select,这两个值将始终不同。 那么我怎样才能让CGRect成为CGPointCGSize呢? 先谢谢你。

Objective-C有两个不同的选项:

 CGRect aRect = CGRectMake(aPoint.x, aPoint.y, aSize.width, aSize.height); CGRect aRect = { aPoint, aSize }; 

Swift 3:

 let aRect = CGRect(origin: aPoint, size: aSize) 

基于@Jim的最佳答案,还可以使用此方法构buildCGPoint和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} }; 
 CGRectMake(yourPoint.x, yourPoint.y, yourSize.width, yourSize.height); 

你可以使用一些糖语法。 例如:

这就像你可以使用更多可读代码的构造块:

 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; )}