CGRectMake() es una función (en realidad una línea # define) que se utiliza mucho, especialmente cuando se crea objetos gráficos de código o interfaz de usuario. o UIImageView . CGRectMake() restituisce una struct (struttura di tipo) CGRect : Su uso se asocia a menudo por lo tanto, los componentes de la inicialización de UIKit , sino también a simples UIView o UIImageView . CGRectMake() devuelve una struct (tipo de estructura) CGRect :
1 2 3 4 5 | struct {CGRect CGPoint origen; CGSize tamaño; }; typedef struct CGRect CGRect; |
: Que a su vez se compone de dos diferentes struct CGPoint y CGSize :
1 2 3 4 5 6 7 8 9 10 11 12 13 | struct {CGPoint CGFloat x; CGFloat y; }; typedef struct CGPoint CGPoint; Tamaños / *. * / struct {CGSize Ancho CGFloat; CGFloat altura; }; typedef struct CGSize CGSize; |
. Que, a su vez, otra vez, contienen tipos CGFloat o el tipo float . Si analizamos el código de CGRectMake () son los siguientes:
1 2 3 4 5 6 7 8 | CG_INLINE CGRect CGRectMake (CGFloat x, y CGFloat, ancho CGFloat, CGFloat altura) { CGRect rect; y; rect.origin.x = x, y = rect.origin.y; height; rect.size.width = anchura, altura = rect.size.height; volver rect; } |
De ello se desprende, por tanto, que este pedazo de código:
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: @ "Prensa" Forst: UIControlStateNormal]; gbutton ] ; [MainWindow addSubview: gbutton]; |
Con razón se puede escribir como:
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: @ "Prensa" Forst: UIControlStateNormal]; gbutton ] ; [MainWindow addSubview: gbutton]; |
Sólo para acelerar las cosas "se ejecuta el código ... ![]()











Curiosidad: Si quieres aprender Obj-C, por ejemplo, cero o casi, en el que las publicaciones se puede "girar?
@ Cristian: búsqueda de libros de Google (Google Books), hay muchos recursos (en Inglés) es prácticamente completa.
Un mal consejo, lo siento. è inline, ma la differenza sarebbe impercettibile anche se fosse una funzione vera e propria). Reduce la encapsulación de código (que pasa si cambias la aplicación de la
CGRectMake? Mis obras y su código necesita ser modificado) y la diferencia es imperceptible en términos de rendimiento (entre otras cosasCGRectMakeestá en línea, pero la diferencia se podría indetectable, incluso si se tratara de una función real).Este consejo representa entre otros anti-patrón de la optimización prematura.
@ Kheimon: Estoy de acuerdo, no hoy no se usaría en un código real, especialmente en relación con
CGRect, dada la all'iOS actualizaciones continuas. Sin embargo, es interesante mostrar cómo las estructuras de ese tipo puede ser necesaria para alinear.