Objective-C: an alternative to using CGRectMake

CGRectMake() is a function (actually an inline # define) used a lot especially when you create graphics objects from code or user interface. o UIImageView . CGRectMake() restituisce una struct (struttura di tipo) CGRect : Its use is therefore often associated initialization components of UIKit , but also to simple UIView or UIImageView . CGRectMake() returns a struct (structure type) CGRect :

1
2
3
4
5
struct {CGRect
CGPoint origin;
CGSize size;
};
typedef struct CGRect CGRect;

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

1
2
3
4
5
6
7
8
9
10
11
12
13
struct {CGPoint
CGFloat x;
CGFloat y;
};
typedef struct CGPoint CGPoint;

/ * Sizes. * /

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

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

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

It follows, therefore, that this piece of code:

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

It could rightly be written as:

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

Just to speed things up 'the code is running ... :)

4 comments to "Objective-C: an alternative to using CGRectMake"

  1. July 27, 2009 Cristian :

    Curiosity ': If you want to learn Obj-C, say, zero or nearly so, in which publications you can' turn?

  2. August 1, 2009 Giovambattista Fazioli :

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

  3. November 10, 2011 kheimon:

    Bad advice, I'm sorry. è inline, ma la differenza sarebbe impercettibile anche se fosse una funzione vera e propria). Reduces the encapsulation of code (which happens if you change the implementation of CGRectMake ? My code works and your needs to be amended) and the difference is imperceptible in terms of performance (among other things CGRectMake is inline, but the difference would be undetectable even if it were a real function).
    This advice embodies among other anti-pattern premature optimization.

  4. November 10, 2011 Giovambattista Fazioli :

    @ Kheimon: I agree, not today would never use in a real code, especially in relation to CGRect , given the continuous updates all'iOS. However it is interesting to show how structs of that type may be needed to linearize.

Leave a comment

XHTML TAG PERMIT: <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 [cc_actionscript][/cc_actionscript] // Actionscript [cc_actionscript3][/cc_actionscript3] // Actionscript 3 [cc_css][/cc_css] // CSS Style Sheet [cc_html][/cc_html] // HTML [cc_js][/cc_js] // Javascript [cc_objc][/cc_objc] // Objective-C [cc_php][/cc_objc] // PHP [cc_sql][/cc_sql] // SQL 


Stop SOPA