Objective-C: an alternative to the use of CGRectMake

Monday, July 27, 2009

CGRectMake() is a function (in reality an inline # define) used a lot especially when you create objects from code or graphical user interface. Its use is therefore often linked initialization components UIKit but also to simple UIView or UIImageView CGRectMake() returns a struct (structure type) CGRect

Objective-C:
  1. struct (CGRect
  2. CGPoint origin;
  3. CGSize size;
  4. );
  5. typedef struct CGRect CGRect;

Which in turn is composed of two different struct CGPoint and CGSize

Objective-C:
  1. struct (CGPoint
  2. CGFloat x;
  3. CGFloat y;
  4. );
  5. typedef struct CGPoint CGPoint;
  6. / * Sizes. * /
  7. struct (CGSize
  8. CGFloat width;
  9. CGFloat height;
  10. );
  11. typedef struct CGSize CGSize;

That, in turn, still contain the kinds CGFloat or type float If we analyze the code of CGRectMake () are:

Objective-C:
  1. CG_INLINE CGRect
  2. CGRectMake (CGFloat x, y CGFloat, CGFloat width, height CGFloat)
  3. (
  4. CGRect rect;
  5. y; rect.origin.x = x; rect.origin.y = y;
  6. height; rect.size.width = width; rect.size.height = height;
  7. return rect;
  8. )

It follows, therefore, that this part of code:

Objective-C:
  1. UIButton buttonWithType : UIButtonTypeRoundedRect ] ; UIButton * gbutton = [UIButton buttonWithType: UIButtonTypeRoundedRect];
  2. 12,409,100,40 ) ; gbutton.frame = CGRectMake (12,409,100,40);
  3. @ "Press" forState : UIControlStateNormal ] ; [Gbutton setTitle: @ "Press" Forst: UIControlStateNormal];
  4. gbutton ] ; [MainWindow addSubview: gbutton];

Could rightly be written as:

Objective-C:
  1. UIButton buttonWithType : UIButtonTypeRoundedRect ] ; UIButton * gbutton = [UIButton buttonWithType: UIButtonTypeRoundedRect];
  2. 12,409,100,40 } ; gbutton.frame = (CGRect) (12,409,100,40);
  3. @ "Press" forState : UIControlStateNormal ] ; [Gbutton setTitle: @ "Press" Forst: UIControlStateNormal];
  4. gbutton ] ; [MainWindow addSubview: gbutton];

Just to speed up a bit 'code execution ... :)

Related Post

Was this article helpful?: Per nientePocoAbbastanzaMoltoMoltissimo
Loading ... Loading ...

2 comments to "Objective-C: an alternative to the use of CGRectMake"

  1. getAvatar 1.0
    July 27, 2009 Cristian:

    Curiosity ': if you want to learn Obj-C, say, from zero or nearly so, to which publications you can' pay?

  2. getAvatar 1.0
    01 ago, 2009 Giovambattista Fazioli:

    @ Cristian: Google Book Search (Google Books), there are many resources (in English) is virtually complete.

Leave a comment

TAG XHTML PERMISSIONS: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> INSERTION CODE:
 <pre></pre>         // blocco generico [code][/code]       // blocco generico [as][/as]           // Actionscript [css][/css]         // CSS Style Sheet [html][/html]       // HTML [js][/js]           // Javascript [objc][/objc]       // Objective-C [php][/php]         // PHP [sql][/sql]         // SQL