【问题标题】:Quartz 2D drawing primitives RotationQuartz 2D 绘图图元 旋转
【发布时间】:2011-12-18 06:41:06
【问题描述】:

我是 Quartz 2D 的新手。我正在尝试绘制一个三角形然后旋转。由于我使用 Quartz 2D 的背景有限,我从 Apple/谷歌搜索中发现我可以使用 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);

【问题讨论】:

    标签: ios quartz-2d


    【解决方案1】:

    好的,很抱歉没有足够快地回放以完成几个决赛。我完全按照你的指示做了,我得到了它的工作。这是代码,也许它会帮助别人

    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

    【讨论】:

      【解决方案2】:

      试试这个:

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

      只有这样

      // Draw Text
      

      【讨论】:

      • 我尝试了这个逻辑,但它不起作用。现在它只显示文本而不是三角形。
      • 如果坐标有问题,请在相关行所在的代码中插入一个断点。或抛出std::cout << yourVariable << "\n"
      【解决方案3】:
      CGContextRotateCTM(context, [data heading]* M_PI/180);
      
      CGContextClosePath(context);
      CGContextDrawPath(context, kCGPathFill);
      
      CGContextRotateCTM(context, -[data heading]* M_PI/180);
      
      // Draw Text
      

      【讨论】:

      • 非常感谢您的帮助。它有点工作。我绘制的三角形看起来旋转正确,但根本不在正确的位置。另一方面,文本绘制在正确的位置。
      • 很抱歉没有尽快回复您!旋转是从左上角开始,平移到场景的中心,然后旋转,然后平移回来,然后绘制1
      猜你喜欢
      • 2013-08-10
      • 2012-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-09
      • 1970-01-01
      相关资源
      最近更新 更多