[英]Custom UIKit drawing with cocos2d (2.0)?
我試圖在我要構建的游戲中在我的cocos層上使用自定義UiView。 當我說自定義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);
}
附加信息:
任何幫助表示贊賞!
因此,經過幾個小時的環顧后,我得到了這個工作。
使用錯誤的上下文來得出我的觀點。
只需刪除此行。
UIGraphicsBeginImageContext(self.bounds.size);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.