Quartz 2D绘图原语旋转

时间:2011-12-18 06:41:06

标签: ios quartz-2d

我是Quartz 2D的新手。我正在尝试画一个三角形然后旋转。由于我使用Quartz 2D的背景有限,我从Apple / Google搜索中发现我可以使用CGContextRotateCTM功能。我的问题是,当我这样做时,我绘制的整个文本也被旋转。我尝试使用CGContextSaveGstate并在完成轮换后恢复但没有工作。我想知道我的方法是否正确?或者我有更好的方法来实现这一目标?

  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 个答案:

答案 0 :(得分: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书有什么建议吗! Apple doc有点帮助,但理解这个概念并不是那么好..etc

答案 1 :(得分:0)

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

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

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

// Draw Text