CGPath

2011-11-29 14 views
5

'u dolduramam Yolumu doldurmayla ilgili yardıma ihtiyacım var. Örneğin CGContextFillPath(path);'u nereye koyacağımı bilmiyorum. Çizim yapmak için oldukça yeni (ve iOS Geliştirme) ve şimdi neredeyse iki saat boyunca her şeyi denedim. Ama ben sadece pes etmem gereken yere vardım ... Umarım benim sorunuma ışık tutabilirsiniz. invalid contextCGPath

-(CGMutablePathRef) drawHexagon:(CGPoint)origin 
    { 
     //create mutable path 
     CGMutablePathRef path = CGPathCreateMutable(); 

     CGPathMoveToPoint(path, nil, origin.x, origin.y); 

     CGPoint newloc = CGPointMake(origin.x - 20, origin.y + 42); 
     CGPathMoveToPoint(path, NULL, newloc.x, newloc.y); 
     CGPathAddLineToPoint(path, NULL, newloc.x + 16,newloc.y + 38); 
     CGPathAddLineToPoint(path, NULL, newloc.x + 49, newloc.y + 0); 
     CGPathAddLineToPoint(path, NULL, newloc.x + 23, newloc.y - 39); 
     CGPathAddLineToPoint(path, NULL, newloc.x - 25,newloc.y - 40); 
     CGPathAddLineToPoint(path, NULL, newloc.x -43, newloc.y + 0); 
     CGPathCloseSubpath(path); 
     CGContextAddPath(context, path); 
     context = UIGraphicsGetCurrentContext(); 
     CGContextSaveGState(context); 
     CGContextSetLineWidth(context, 2.0); 
     CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); 
     CGContextAddPath(context, path); 
     CGContextStrokePath(context); 
     CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor); 
     CGContextFillPath(path); 



      return path; 

    } 

    -(id) init 
    { 
     // always call "super" init 
     // Apple recommends to re-assign "self" with the "super" return value 
     if((self=[super init])) { 

      clickedHexagonsUpToNow = [[NSMutableArray alloc]init ]; 
      CGSize screenSize = [CCDirector sharedDirector].winSize; 

      background = [CCSprite spriteWithFile:@"background.png"]; 
      background.anchorPoint= ccp(0,0); 
      [self addChild:background z:-1 tag:0]; 

      hex1TouchArea = [self drawHexagon:ccp(82,120)]; 
      hex2TouchArea = [self drawHexagon:ccp(50,157)]; 
      hex3TouchArea = [self drawHexagon:ccp(220 ,196)]; 
      hex4TouchArea = [self drawHexagon:ccp(280,153)]; 
      hex5TouchArea = [self drawHexagon:ccp(84,118)]; 
      hex6TouchArea = [self drawHexagon:ccp(82,120)]; 
      hex7TouchArea = [self drawHexagon:ccp(82,120)]; 
      hex8TouchArea = [self drawHexagon:ccp(82,120)]; 
      hex9TouchArea = [self drawHexagon:ccp(82,120)]; 


      self.isTouchEnabled = YES; 

      } 
    return self; 
} 

cevap

7

Bu parametre, bir CGPath gibi bir CGContext almalıdır: aşağıdaki hatayı alıyorum. CGContextFillPath hiçbir şey yapacağız böylece

CGContextFillPath(context); 

Ancak CGContextStrokePath çağrısı geçerli yolunu temizledi. inme ve aynı yolda doldurun sen CGContextDrawPath kullanmalısınız:

... 
    CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); 
    CGContextAddPath(context, path); 
// CGContextStrokePath(context); 
    CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor); 
// CGContextFillPath(path); 
    CGContextDrawPath(context, kCGPathFillStroke); 

(ve ayrıca bağlamını alma hakkında @Luiz 'ın değişikliği uygulamak.)

+0

(Mümkün) aptal soru için özür dilerim, ama son satırdaki 'k' orada ne yapıyor? – 11684

+0

Kendi sorumu yanıtladı! – 11684

+0

Bu sadece kontur rengini çizer, hiçbir zaman dolgu rengini çizmez ... – jjxtra

1

Sen bir yol eklemeden önce bağlamı almak gerekir o:

CGContextFillPath(context); 
:

Ayrıca
context = UIGraphicsGetCurrentContext();    
CGContextAddPath(context, path); 

, yolunu doldurmak için, size yol kendisi bağlam referansı değil, geçmesi gerekiyor

+0

Hala bunu dolduramıyorum ... :( –