49

我需要从不同的andUIImageView集合中为 a 创建一个框架,这两个值将始终根据用户的选择而有所不同。那么如何制作表格 a和 a ?先感谢您。CGSizeCGPointCGRectCGPointCGSize

4

4 回答 4

122

Objective-C 的两个不同选项:

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

CGRect aRect = { aPoint, aSize };

斯威夫特 3:

let aRect = CGRect(origin: aPoint, size: aSize)
于 2012-08-21T21:59:22.150 回答
5

基于@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} };
于 2014-12-11T21:06:25.513 回答
2
CGRectMake(yourPoint.x, yourPoint.y, yourSize.width, yourSize.height);
于 2012-08-21T21:57:54.417 回答
0

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

这类似于构造块,您可以将其用于更易读的代码:

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;
)}
于 2016-01-19T18:56:41.353 回答