tags:

views:

63

answers:

3

Given that drawRect takes a CGRect 'rect', how can I create 4 separate CGFloat variables with the individual values of rect (x,y,width,height)? I need to use the variables on an individual basis to determine certain drawing scales within the drawRect method.

- (void)drawRect:(CGRect)rect {
 CGFLoat x = ***x value of rect***;
 CGFLoat y = ***y value of rect***;
 CGFloat width = ***width value of rect***;
 CGFloat height = ***height value of rect***;
 }

Thanks in advance for your help!

+4  A: 
 CGFLoat x = rect.origin.x;
 CGFLoat y = rect.origin.y;
 CGFloat width = rect.size.width;
 CGFloat height = rect.size.height;
Dave DeLong
Use the Functions, Man!
bbum
nice one, thanks!
Timbo
A: 

The structure of a CGRect is defined as such

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

Which means you can do this:

rect.origin.x
rect.origin.y
rect.size.width
rect.size.height
slf
Bit of an incomplete declaration, thar. Assumes you know what a CGPoint and CGSize are.
bbum
agreed, your answer is much more complete :)
slf
+3  A: 

I'd suggest you brush up on some basic C materials as the question is pretty basic C structure stuff (no big deal -- we were all there once).

Given:

struct CGPoint {
  CGFloat x;
  CGFloat y;
};
typedef struct CGPoint CGPoint;

/* Sizes. */

struct CGSize {
  CGFloat width;
  CGFloat height;
};
typedef struct CGSize CGSize;

/* Rectangles. */

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

You want: - (void)drawRect:(CGRect)rect { CGFLoat x = rect.origin.x; CGFLoat y = rect.origin.y; CGFloat width = rect.size.width; CGFloat height = rect.size.height; }

Or, better yet, because there are functions for such madness:

- (void)drawRect:(CGRect)rect {
     CGFLoat x = CGRectGetMinX(rect);
     CGFLoat y = CGRectGetMinY(rect);
     CGFloat width = CGRectGetWidth(rect);
     CGFloat height = CGRectGetHeight(rect);
 }
bbum
Excellent, thanks! I know my skills are rusty, so much to learn
Timbo
Yup -- no worries -- like I said, we've all been there. Xcode generally does a wonderful job of taking you to the definition of something if you cmd-dbl-click on the type.
bbum