我试图在我正在尝试构建的游戏中在我的 cocos 层上使用自定义 UiViews。当我说自定义 UIView 时,我想在 UIView 的绘制矩形中编写并使用我自己的绘图代码来创建视图(使用贝塞尔路径)。虽然我知道如何将 UiView 添加到屏幕上,如下所示:
[[[CCDirector sharedDirector] view] addSubview:myView];
我的 drawRect 方法中的绘图代码似乎不起作用!
这就是我设置 cocos2d 的方式:
- (void)setupCocos2D {
director = (CCDirectorIOS*) [CCDirector sharedDirector];
CCGLView *glView = [CCGLView viewWithFrame:[[UIScreen mainScreen] bounds]
pixelFormat:kEAGLColorFormatRGB565 //kEAGLColorFormatRGBA8
depthFormat:0 //GL_DEPTH_COMPONENT24_OES
preserveBackbuffer:NO
sharegroup:nil
multiSampling:YES
numberOfSamples:1024];
[director setView:glView];
[glView setMultipleTouchEnabled:NO];
[director enableRetinaDisplay:NO];
director.wantsFullScreenLayout = YES;
[director setDisplayStats:YES];
[director setDepthTest:YES];
[director setAnimationInterval:1.0/60];
}
以下是我在 myView 的 draw rect 方法中的自定义绘图代码:
- (void)drawRect:(CGRect)rect
{ //// General Declarations
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
UIGraphicsBeginImageContext(self.bounds.size);
context = UIGraphicsGetCurrentContext();
//// Color Declarations
UIColor* defaultMidGreen_COLOR = [UIColor colorWithRed: 0.231 green: 0.392 blue: 0.392 alpha: 1];
UIColor* panelGradient_COLOR = [UIColor colorWithRed: 0.231 green: 0.392 blue: 0.392 alpha: 1];
UIColor* default_0Opacity_COLOR = [UIColor colorWithRed: 0.231 green: 0.392 blue: 0.392 alpha: 0];
UIColor* defaultDarkGreen_60Opacity_COLOR = [UIColor colorWithRed: 0.231 green: 0.392 blue: 0.392 alpha: 0.6];
UIColor* iconPlayer_Fill_COLOR = [UIColor colorWithRed: 0.4 green: 0.8 blue: 0.769 alpha: 1];
//// Gradient Declarations
NSArray* bottom_GRADIENTColors = [NSArray arrayWithObjects:
(id)panelGradient_COLOR.CGColor,
(id)[UIColor colorWithRed: 0.231 green: 0.392 blue: 0.392 alpha: 0.5].CGColor,
(id)default_0Opacity_COLOR.CGColor, nil];
CGFloat bottom_GRADIENTLocations[] = {0, 0.55, 1};
CGGradientRef bottom_GRADIENT = CGGradientCreateWithColors(colorSpace, (CFArrayRef)bottom_GRADIENTColors, bottom_GRADIENTLocations);
{
//// panelComponent-BottomSolid Drawing
solidPaths = [[UIBezierPath alloc] init];
[solidPaths moveToPoint: CGPointMake(0, 110)];
[solidPaths addLineToPoint: CGPointMake(540, 110)];
[solidPaths addLineToPoint: CGPointMake(540, 60)];
[solidPaths addLineToPoint: CGPointMake(0, 60)];
[solidPaths addLineToPoint: CGPointMake(0, 110)];
[solidPaths closePath];
[defaultMidGreen_COLOR setFill];
[solidPaths fill];
//// panelComponent-BottomGradient Drawing
gradientPaths = [[UIBezierPath alloc] init];
[gradientPaths moveToPoint: CGPointMake(0, 2)];
[gradientPaths addLineToPoint: CGPointMake(539, 2)];
[gradientPaths addLineToPoint: CGPointMake(539, 60)];
[gradientPaths addLineToPoint: CGPointMake(0, 60)];
[gradientPaths addLineToPoint: CGPointMake(0, 2)];
[gradientPaths closePath];
CGContextSaveGState(context);
[gradientPaths addClip];
outline = [[UIBezierPath alloc ]init];
[outline moveToPoint: CGPointMake(1, 109)];
[outline addLineToPoint: CGPointMake(539, 109)];
[outline addLineToPoint: CGPointMake(539, 0)];
[outline addLineToPoint: CGPointMake(1, 0)];
[outline addLineToPoint: CGPointMake(1, 109)];
[outline closePath];
[defaultDarkGreen_60Opacity_COLOR setFill];
[iconPlayer_Fill_COLOR setStroke];
outline.lineWidth = 1;
gripLines = [[UIBezierPath alloc] init];
[gripLines moveToPoint: CGPointMake(200, 3)];
[gripLines addLineToPoint: CGPointMake(200, 2)];
[gripLines addLineToPoint: CGPointMake(340, 2)];
[gripLines addLineToPoint: CGPointMake(340, 3)];
[gripLines addLineToPoint: CGPointMake(200, 3)];
[gripLines closePath];
[iconPlayer_Fill_COLOR setFill];
[gripLines fill];
}
[solidPaths stroke];
[gripLines stroke];
[outline stroke];
//// Cleanup
UIGraphicsEndImageContext();
CGGradientRelease(bottom_GRADIENT);
CGColorSpaceRelease(colorSpace);
}
附加信息:
- 如果我尝试在 draw rect 中设置 backgroundColor 属性,它就不起作用。但是,如果我用颜色初始化对象(在 init 方法中),同样的工作
- 我认为这与我尝试使用的图形上下文以及我正在使用的图形上下文和 cocos2D 正在使用的图形上下文之间存在冲突有关!虽然不确定!
任何帮助表示赞赏!