This is a very simple question of which i can't seem to find the answer. How to do this :
CGRectMake(1, 3, size)
or
CGRectMake(pointB, size)
or
CGRectMake(pointB, size.width, size.height)
instead of
CGRectMake(self.someview.frame.origin.x, self.someview.frame.origin.y, self.someotherview.frame.size.width, self.someotherview.frame.size.height)
?? Thank you ! :)
EDIT:
The method CGRectmake
takes CGFloat
. I would like it to take CGSize
, and/or CGpoint
as arguments of the method.
Overview. A CGSize structure is sometimes used to represent a distance vector, rather than a physical size. As a vector, its values can be negative. To normalize a CGRect structure so that its size is represented by positive values, call the CGRectStandardize(_:) function.
A structure that contains the location and dimensions of a rectangle.
CGSize is a structure: struct CGSize { CGFloat width; CGFloat height; }; typedef struct CGSize CGSize; Just use tagStringLength. width to get the number you care about.
I think this is what you have in mind:
- (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));
}
But note the discussion here:
Why use functions like CGRectMake?
This kind of compound literal syntax seems to me much easier to read and write, although functions have the edge when it comes to futureproofing ( + you get autocomplete).
update
see also this more recent q&a:
CGRect syntax I haven't seen before
CGRectMake(1, 3, size):
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)
A CGRect just looks like this:
struct CGRect {
CGPoint origin;
CGSize size;
};
typedef struct CGRect CGRect;
And CGPoint and CGSize just look like this:
struct CGPoint {
CGFloat x;
CGFloat y;
};
typedef struct CGPoint CGPoint;
struct CGSize {
CGFloat width;
CGFloat height;
};
typedef struct CGSize CGSize;
CGRectMake is the following function:
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;
}
So instead of:
CGRect r = CGRectMake(pointB.x, pointB.y, size.width, size.height)
You can simply write:
CGRect r;
r.origin = pointB;
r.size = size;
If you feel like creating your own CGRectMake, feel free to do so:
CG_INLINE CGRect
MyPrivateCGRectMake(CGPoint p, CGSize s)
{
CGRect rect;
rect.origin = p;
rect.size = s;
return rect;
}
But there is no way you can change which arguments an already existing function accepts.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With