Define CGRectmake using CGPoint and / or CGSize

This is a very simple question, about which I can not find the answer. How to do it:

CGRectMake(1, 3, size) 

or

 CGRectMake(pointB, size) 

or

  CGRectMake(pointB, size.width, size.height) 

instead

 CGRectMake(self.someview.frame.origin.x, self.someview.frame.origin.y, self.someotherview.frame.size.width, self.someotherview.frame.size.height) 

?? Thanks!:)

EDIT:

The CGRectmake method accepts CGFloat . I would like it to accept CGSize and / or CGpoint as method arguments.

+4
source share
2 answers

I think this is what you mean:

  - (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 pay attention to the discussion here:
Why use features like CGRectMake?

This type of compound syntax literal is much easier for me to read and write, although functions have an advantage when it comes to future protection (+ you get autocomplete).

Update
see also this later q & a:

CGRect syntax I haven't seen before

+6
source

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) 

CGRect looks something like this:

 struct CGRect { CGPoint origin; CGSize size; }; typedef struct CGRect CGRect; 

Both CGPoint and CGSize 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 want to create your own CGRectMake, feel free to:

 CG_INLINE CGRect MyPrivateCGRectMake(CGPoint p, CGSize s) { CGRect rect; rect.origin = p; rect.size = s; return rect; } 

But you cannot change what arguments an existing function takes.

+6
source

All Articles