如何在CGContextSetFillColorWithColor中访问UIColor的CGColor属性?

Aze*_*rai 15 iphone objective-c cgcolor uicolor

CGContextSetFillColorWithColor(g, [UIColor greyColor].CGColor);
Run Code Online (Sandbox Code Playgroud)

我正在尝试关注O'Reilly的书,iPhone Game Development,但是在第73页第3章我得到了这个错误:

error: request for member 'CGColor' in something not a structure or union
Run Code Online (Sandbox Code Playgroud)

根据这本书的勘误页面,这是一本未经证实的勘误表.该行可以替换哪些功能代码?

额外细节

示例项目可以在这里下载.

我在渲染函数中遇到错误,遵循本书第72页到第73页的说明,在gsMain.m的渲染函数中构建gsMain类(它与示例项目pg77不同)

本书指示构建gsMain类的代码片段如下:

//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
Run Code Online (Sandbox Code Playgroud)

Chapter3_Example_p77应该显示从第71页到第77页的练习结果,但它与71到77中给出的给定指令非常不同.以下代码是从上面链接下载的已完成的可编译类.

//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
Run Code Online (Sandbox Code Playgroud)

nie*_*bot 23

怎么样[ [ UIColor grayColor ] CGColor ]

您使用的是最新的SDK吗?我假设你的UIColor.h标题有一个属性CGColor?(我会检查你的SDK设置 - 当我打开示例项目时,它被设置为iOS 3.x)


在Swift 3.x中: UIColor.gray.cgColor


Ale*_*lds 6

p77为Simulator 3.1.3 编译了示例,并没有遇到任何编译器警告或错误.

代码:

- (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]]];

    //...
}
Run Code Online (Sandbox Code Playgroud)

似乎编译好了.也许您可以指定您正在编译的三个示例中的哪一个以及目标操作系统版本?


ken*_*ytm 5

确保你#include <UIKit/UIKit.h>.

-grayColor不是-greyColor.