2011-12-18 41 views
2

我是Quartz 2D的新手。我試圖繪製一個三角形然後旋轉。有了我使用Quartz 2D的有限背景,我從Apple/Google搜索中發現我可以使用CGContextRotateCTM函數。我的問題是,當我這樣做後,我畫的整個文本也旋轉。我嘗試使用CGContextSaveGstate並在我做旋轉後恢復它,但沒有工作。我想知道我的方法是否正確?或者有更好的方法可以用來實現這個目標?Quartz 2D drawing primitives旋轉

CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSaveGState(context); 
    for (key in data) 
    { 
    // get point 
    Data *tmpdata =[data objectForKey:key] ; 
    point=[data point ] 
    //setup and draw the 
    CGContextBeginPath(context); 
    CGContextSetRGBFillColor(context, [data fillcolor].r, 
     [data fillcolor].g, [tmpACdata fillcolor].b, 1); 
    CGContextSetLineWidth(context, 2.0 

    // Draw Triangle   
    CGContextMoveToPoint(context,point.x,point.y); 
    CGContextAddLineToPoint(context, point.x+8, point.y+8); 
    CGContextAddLineToPoint(context, point.x-8, point.y+8); 

    CGContextClosePath(context); 
    CGContextDrawPath(context, kCGPathFill); 

    CGContextRotateCTM(context, [data heading]* M_PI/180); 

    CGContextClosePath(context); 
    CGContextDrawPath(context, kCGPathFill); 
    // Draw Text 
    ............... 
    } 
    CGContextRestoreGState(context); 

回答

2

試試這個:

CGContextSaveGState(context); 
CGContextRotateCTM(context, [data heading]* M_PI/180); 
// draw triangle 
CGContextRestoreGState(context); 

才把

// Draw Text 
+0

我試過了邏輯,它沒有工作。現在它只顯示文字,但不顯示三角形。 – user519274 2011-12-18 10:26:49

+0

如果您在座標方面有問題,請在相關行所在的代碼處插入一段代碼。或者拋出一個'std :: cout << yourVariable <<「\ n」'。 – Adrian 2011-12-18 10:32:34

0
CGContextRotateCTM(context, [data heading]* M_PI/180); 

CGContextClosePath(context); 
CGContextDrawPath(context, kCGPathFill); 

CGContextRotateCTM(context, -[data heading]* M_PI/180); 

// Draw Text 
+0

我真的很感謝你的幫助。其種類繁多。我得到的三角形繪製似乎與正確的旋轉,但不是在適當的位置。另一方面,文本繪製在正確的位置。 – user519274 2011-12-18 22:23:55

+0

抱歉沒有及早回覆您!旋轉是從左上角開始,轉換到場景的中心,然後旋轉,然後迴轉,然後draw1 – Nico 2011-12-19 04:01:00

3

好,對不起,不重播回速度不夠快被整理夫婦決賽。我完全按照你指出的那樣做了,並且讓它工作。這裏是代碼也許它會幫助別人

CGContextSaveGState(context); 
CGContextBeginPath(context);   
CGContextTranslateCTM(context, point.x, point.y); 
//CGContextScaleCTM(context, 1.0, -1.0); that didnt work   
CGContextRotateCTM(context, [data heading]* M_PI/180) ; 
CGContextSetRGBFillColor(context, [data fillcolor].r, [data fillcolor].g, [data fillcolor].b, 1); 

//Draw Triangle 
CGContextMoveToPoint(context,0,0); 
CGContextAddLineToPoint(context, 10, 10); 
CGContextAddLineToPoint(context, 0, 6); 
CGContextAddLineToPoint(context, -10,10);    
CGContextRotateCTM(context,(-1.0)* [tmpACdata heading]* M_PI/180); 
CGContextClosePath(context); 
CGContextDrawPath(context, kCGPathFill);' 
CGContextRestoreGState(context); 

感謝您的幫助,這是偉大的。你有沒有關於一本好實用Quartz 2D書籍的建議! 蘋果文檔有點幫助,但不是很瞭解這個概念..等等