这是一个非常简单的问题,我似乎无法找到答案。 怎么做:
CGRectMake(1, 3, size)
或
CGRectMake(pointB, size)
或
CGRectMake(pointB, size.width, size.height)
而不是
CGRectMake(self.someview.frame.origin.x, self.someview.frame.origin.y, self.someotherview.frame.size.width, self.someotherview.frame.size.height)
?? 谢谢 ! :)
编辑:
方法CGRectmake
需要CGFloat
。我希望将CGSize
和/或CGpoint
作为方法的参数。
答案 0 :(得分:8)
我认为这就是你的想法:
- (void) logRects
{
CGFloat
x = 10.0,
y = 20.0,
width = 50.0,
height = 60.0;
CGPoint point = {x, y};
CGSize size = {width, height};
CGRect rect1 = {1, 3, size};
CGRect rect2 = {point, size};
CGRect rect3 = {point, size.width, size.height};
//using designated (named) initialisers
CGRect rect4 = {.origin.x=3, .origin.y=5, .size = {100,100}};
//with designated initialisers, order doesn't matter
CGRect rect5 = {.size=size, .origin.x=3, .origin.y=5};
NSLog (@"rect1 %@",NSStringFromCGRect(rect1));
NSLog (@"rect2 %@",NSStringFromCGRect(rect2));
NSLog (@"rect3 %@",NSStringFromCGRect(rect3));
NSLog (@"rect4 %@",NSStringFromCGRect(rect4));
NSLog (@"rect5 %@",NSStringFromCGRect(rect5));
}
但请注意这里的讨论:
Why use functions like CGRectMake?
这种复合文字语法在我看来更容易阅读和写入,虽然函数在未来的防范方面具有优势(+你得到自动完成)。
更新
另见最近的q& a:
答案 1 :(得分:6)
CGRectMake(1,3,大小):
CGRectMake(1, 3, size.width, size.heigh)
CGRectMake(pointB,size):
CGRectMake(pointB.x, pointB.y, size.width, size.height)
CGRectMake(pointB,size.width,size.height):
CGRectMake(pointB.x, pointB.y, size.width, size.height)
CGRect看起来像这样:
struct CGRect {
CGPoint origin;
CGSize size;
};
typedef struct CGRect CGRect;
CGPoint和CGSize看起来像这样:
struct CGPoint {
CGFloat x;
CGFloat y;
};
typedef struct CGPoint CGPoint;
struct CGSize {
CGFloat width;
CGFloat height;
};
typedef struct CGSize CGSize;
CGRectMake是以下功能:
CG_INLINE CGRect
CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat height)
{
CGRect rect;
rect.origin.x = x;
rect.origin.y = y;
rect.size.width = width;
rect.size.height = height;
return rect;
}
所以而不是:
CGRect r = CGRectMake(pointB.x, pointB.y, size.width, size.height)
您可以简单地写一下:
CGRect r;
r.origin = pointB;
r.size = size;
如果您想创建自己的CGRectMake,请随意执行此操作:
CG_INLINE CGRect
MyPrivateCGRectMake(CGPoint p, CGSize s)
{
CGRect rect;
rect.origin = p;
rect.size = s;
return rect;
}
但是你无法改变现有函数接受的参数。