首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在CGColor中访问UIColor的UIColor属性?

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

Stack Overflow用户
提问于 2010-05-02 08:50:53
回答 3查看 29.9K关注 0票数 15
代码语言:javascript
复制
CGContextSetFillColorWithColor(g, [UIColor greyColor].CGColor);

我试图遵循O‘’Reilly的书,iPhone游戏开发,但在第73页第3章我得到了这个错误:

代码语言:javascript
复制
error: request for member 'CGColor' in something not a structure or union

根据这本书的errata页面,这是书中一个未经证实的错误。那行可以用什么功能代码替换呢?

附加细节

示例项目可以下载这里

我在呈现函数中遇到了错误,我按照书中从第72页到第73页的说明,在gsMain.m的呈现函数上构建gsMain类(它不同于示例项目pg77)。

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

代码语言:javascript
复制
//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

Chapter3_Example_p77应该显示从第71页到77页的练习结果,但是它与71到77页中给出的指示有很大的不同。下面的代码是从上述链接下载的已完成的可编译类。

代码语言:javascript
复制
//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
EN

回答 3

Stack Overflow用户

发布于 2013-12-31 09:05:12

[ [ UIColor grayColor ] CGColor ]

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

在Swift 3.x:UIColor.gray.cgColor

票数 23
EN

Stack Overflow用户

发布于 2010-05-02 09:01:52

我为Simator3.1.3编译了p77示例,没有遇到任何编译器警告或错误。

守则:

代码语言:javascript
复制
- (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]]];

    //...
}

好像编译好了。也许您可以指定您正在编译的三个示例中的哪个,以及针对哪个目标OS版本?

票数 6
EN

Stack Overflow用户

发布于 2010-05-02 10:05:43

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

这是-grayColor,而不是-greyColor

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2752898

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档