【发布时间】:2012-08-17 07:37:03
【问题描述】:
我需要从CGSize 和CGPoint 的不同集合中为UIImageView 创建一个框架,这两个值总是会根据用户的选择而有所不同。那么我怎样才能使CGRect 形成CGPoint 和CGSize?提前谢谢你。
【问题讨论】:
标签: ios objective-c quartz-graphics cgpoint cgrectmake
我需要从CGSize 和CGPoint 的不同集合中为UIImageView 创建一个框架,这两个值总是会根据用户的选择而有所不同。那么我怎样才能使CGRect 形成CGPoint 和CGSize?提前谢谢你。
【问题讨论】:
标签: ios objective-c quartz-graphics cgpoint cgrectmake
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)
【讨论】:
基于@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} };
【讨论】:
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;
)}
【讨论】: