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
- struct (CGRect
- CGPoint origin;
- CGSize size;
- );
- typedef struct CGRect CGRect;
Which in turn is composed of two different struct CGPoint and CGSize
- struct (CGPoint
- CGFloat x;
- CGFloat y;
- );
- typedef struct CGPoint CGPoint;
- / * Sizes. * /
- struct (CGSize
- CGFloat width;
- CGFloat height;
- );
- typedef struct CGSize CGSize;
That, in turn, still contain the kinds CGFloat or type float If we analyze the code of CGRectMake () are:
- CG_INLINE CGRect
- CGRectMake (CGFloat x, y CGFloat, CGFloat width, height CGFloat)
- (
- CGRect rect;
- y; rect.origin.x = x; rect.origin.y = y;
- height; rect.size.width = width; rect.size.height = height;
- return rect;
- )
It follows, therefore, that this part of code:
- 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];
Could rightly be written as:
- UIButton buttonWithType : UIButtonTypeRoundedRect ] ; UIButton * gbutton = [UIButton buttonWithType: UIButtonTypeRoundedRect];
- 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 up a bit 'code execution ... ![]()














Curiosity ': if you want to learn Obj-C, say, from zero or nearly so, to which publications you can' pay?
@ Cristian: Google Book Search (Google Books), there are many resources (in English) is virtually complete.