Я пытаюсь использовать пользовательские UiView над моими слоями кокоса в игре, которую я пытаюсь построить. Когда я говорю пользовательский UIView, я хочу написать и использовать свой собственный код чертежа внутри draw UIView для создания представления (используя пути безье). В то время как я знаю, как добавить UIView на экран, например, так: КодПользовательский рисунок UIKit с cocos2d (2.0)?
[[[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 Проведем 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);
}
Дополнительная информация:
- Если я попытаюсь установить b ackgroundColor свойство внутри draw rect, оно не работает. Однако то же самое работает, если я инициализирую объект цветом (в методе init)
- Я думаю, что он имеет что-то с графическим контекстом, который я пытаюсь использовать, и конфликт между тем, который я использую, и один cocos2D с помощью! Не уверен, хотя!
Любая помощь приветствуется!
Вы подтвердили, что ваш пользовательский чертеж работает в обычном проекте UIKit (без cocos2d)? – LearnCocos2D
Ive возился с изменением графического контекста, и я вижу некоторые рисунки на экране. Может быть, потому, что рисунок UIKit не может использовать те, которые созданы кокосами, и если вы видите в коде i ссылку на контекст «currentGraphics»! Каков правильный способ создания и использования нового графического контекста? Это может быть решением проблемы! – Sagar