iphone objective-c uicolor cgcolor

iphone - ¿Cómo acceder a la propiedad CGColor de UIColor en CGContextSetFillColorWithColor?



objective-c (3)

¿Qué hay de [ [ UIColor grayColor ] CGColor ] ?

¿Estás utilizando el último SDK? Supongo que el encabezado UIColor.h tiene una propiedad para CGColor ? (Revisaría la configuración de su SDK: cuando abrí el proyecto de muestra, estaba configurado en iOS 3.x)

En Swift 3.x: UIColor.gray.cgColor

CGContextSetFillColorWithColor(g, [UIColor greyColor].CGColor);

Estoy intentando seguir el libro de O''Reilly, iPhone Game Development, pero en la página 73 Capítulo 3, recibo este error:

error: request for member ''CGColor'' in something not a structure or union

Según la página de erratas del libro, esta es una errata no confirmada en el libro. ¿Con qué código funcional se puede reemplazar esa línea?

Detalles adicionales

El proyecto de ejemplo se puede descargar here .

Me encontré con el error en la función de render al seguir las instrucciones del libro de la página 72 a la página 73 para crear la clase gsMain (es diferente del proyecto de ejemplo pg77) en la función de render de gsMain.m

El fragmento de código que el libro le indica para construir la clase gsMain es el siguiente:

//gsMain.h @interface gsTest : GameState { } @end //gsMain.m @implementation gsMain -(gsMain*) initWithFrame:(CGRect)frame andManager:(GameStateManager*)pManager { if (self = [super initWithFrame:frame andManager:pManager]) { NSLog(@"gsTest init"); } return self; } -(void) Render { CGContextRef g = UIGraphicsGetCurrentContext(); //fill background with gray CGContextSetFillColorWithColor(g, [UIColor greyColor].CGColor); //Error Occurs here CGContextFillRect(g, CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)); //draw text in black CGContextSetFillColorWithColor(g, [UIColor blackColor].CGColor); [@"O''Reilly Rules!" drawAtPoint:CGPointMake(10.0,20.0) withFont:[UIFont systemFontOfSize:[UIFont systemFontSize]]]; } -(void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event { UITouch* touch = [touches anyObject]; NSUInteger numTaps = [touch tapCount]; //todo: implement touch event code here } @end

Se supone que el Capítulo3_Ejemplo_p77 muestra el resultado de los ejercicios de la página 71 a 77, pero es muy diferente de las instrucciones dadas en 71 a 77. El siguiente código es la clase compilable completada descargada desde el enlace anterior.

//gsMain.h #import <Foundation/Foundation.h> #import "GameState.h" @interface gsMain : GameState { } @end // gsMain.m // Example // Created by Joe Hogue and Paul Zirkle #import "gsMain.h" #import "gsTest.h" #import "Test_FrameworkAppDelegate.h" @implementation gsMain -(gsMain*) initWithFrame:(CGRect)frame andManager:(GameStateManager*)pManager { if (self = [super initWithFrame:frame andManager:pManager]) { //do initializations here. } return self; } - (void) Render { [self setNeedsDisplay]; //this sets up a deferred call to drawRect. } - (void)drawRect:(CGRect)rect { CGContextRef g = UIGraphicsGetCurrentContext(); //fill background with gray CGContextSetFillColorWithColor(g, [UIColor grayColor].CGColor); CGContextFillRect(g, CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)); //draw text in black. CGContextSetFillColorWithColor(g, [UIColor blackColor].CGColor); [@"O''Reilly Rules!" drawAtPoint:CGPointMake(10.0, 20.0) withFont: [UIFont systemFontOfSize: [UIFont systemFontSize]]]; //fps display from page 76 of iPhone Game Development int FPS = [((Test_FrameworkAppDelegate*)m_pManager) getFramesPerSecond]; NSString* strFPS = [NSString stringWithFormat:@"%d", FPS]; [strFPS drawAtPoint:CGPointMake(10.0, 60.0) withFont:[UIFont systemFontOfSize: [UIFont systemFontSize]]]; } //this is missing from the code listing on page 77. -(void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event { UITouch* touch = [touches anyObject]; NSUInteger numTaps = [touch tapCount]; if( numTaps > 1 ) { [m_pManager doStateChange:[gsTest class]]; } } @end


Asegúrese de #include <UIKit/UIKit.h> .

Y es -grayColor , no -greyColor .


p77 ejemplo p77 para Simulator 3.1.3 y no encontré ninguna advertencia o error en el compilador.

El código:

- (void)drawRect:(CGRect)rect { CGContextRef g = UIGraphicsGetCurrentContext(); //fill background with gray CGContextSetFillColorWithColor(g, [UIColor grayColor].CGColor); CGContextFillRect(g, CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)); //draw text in black. CGContextSetFillColorWithColor(g, [UIColor blackColor].CGColor); [@"O''Reilly Rules!" drawAtPoint:CGPointMake(10.0, 20.0) withFont:[UIFont systemFontOfSize:[UIFont systemFontSize]]]; //... }

Parece que se compila bien. ¿Quizás podría especificar cuál de los tres ejemplos está compilando y para qué versión de OS de destino?