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 ... ![]()











Curiosity ': If you want to learn Obj-C, say, zero or nearly so, in which publications you can' turn?
@ Cristian: search on Google Book (Google Books), there are many resources (in English) is virtually complete.
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 thingsCGRectMakeis inline, but the difference would be undetectable even if it were a real function).This advice embodies among other anti-pattern premature optimization.
@ 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.