简体   繁体   English

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

[英]How to access the CGColor property of UIColor in CGContextSetFillColorWithColor?

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

I'm trying to follow O'Reilly's book, iPhone Game Development, but on page 73 Chapter 3 I get this error: 我正在尝试关注O'Reilly的书,iPhone游戏开发,但是在第73页第3章我得到了这个错误:

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

According to the book's errata page this is an unconfirmed errata in the book. 根据这本书的勘误页面,这是一本未经证实的勘误表。 What functional code can that line be replaced with? 该行可以替换哪些功能代码?

Additional Details 额外细节

The example project can be downloaded here . 示例项目可以在这里下载。

I was encountering the error at the render function by following the book's instructions from page 72 to page 73 to build the gsMain class (its different from the example project pg77) at the render function of gsMain.m 我在渲染函数中遇到错误,遵循本书第72页到第73页的说明,在gsMain.m的渲染函数中构建gsMain类(它与示例项目pg77不同)

The code snippet that the book instructs to build gsMain class is as followed: 本书指示构建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

The Chapter3_Example_p77 is supposed to show the result of the exercises from page 71 to 77 but it's very different from the given instructions given in 71 to 77. The following code is the completed, compilable class downloaded from the above link. 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

What about [ [ UIColor grayColor ] CGColor ] ? [ [ UIColor grayColor ] CGColor ]怎么样?

Are you using the latest SDK? 您使用的是最新的SDK吗? I assume your UIColor.h header has a property for CGColor ? 我假设你UIColor.h头有一个属性CGColor (I would check your SDK settings--when I opened the sample project it was set to iOS 3.x) (我会检查你的SDK设置 - 当我打开示例项目时,它被设置为iOS 3.x)


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

I compiled the p77 example for Simulator 3.1.3 and did not run into any compiler warnings or errors. 我为Simulator 3.1.3编译了p77示例,并没有遇到任何编译器警告或错误。

The code: 代码:

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

    //...
}

appears to compile okay. 似乎编译好了。 Perhaps you might specify which of the three examples you're compiling and for what target OS version? 也许您可以指定您正在编译的三个示例中的哪一个以及目标操作系统版本?

Make sure you #include <UIKit/UIKit.h> . 确保#include <UIKit/UIKit.h>

And it is -grayColor , not -greyColor . 它是-grayColor ,而不是-greyColor

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM