2012-08-24 34 views
1

我在drawRect中绘制的图形存储在CGMutablePathRefshapeMutablePath)中。每次调用drawRect时,形状都会拉伸以适应屏幕周围的笔划边框。我想知道,如何绘制笔画边界而又不拉伸呢?即拉伸shapeMutablePath,然后在它周围绘制笔画边框,使其每次绘制时都是相同的宽度?我试图改变规模的顺序和添加和绘制路径无济于事。使用CGContextScaleCTM时避免拉伸描边

- (void) drawRect:(CGRect)rect 
{ 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSaveGState(context); 

    CGContextSetRGBFillColor(context, 1.0000, 1.0000, 1.0000, 1.0000); 
    CGContextSetRGBStrokeColor(context,0.0000,0.0000,0.0000,1.0000); 
    CGContextSetLineWidth(context, DialogueTextViewLineWidth); 

    CGContextScaleCTM (context, self.frame.size.width/self.shapeMutablePathWidth, self.frame.size.height/self.shapeMutablePathHeight); 
    CGContextAddPath(context, self.shapeMutablePath); 
    CGContextDrawPath(context, kCGPathFillStroke); 
    CGContextRestoreGState(context);  
} 

回答

2

相反缩放CTM和使用原始路径:

CGContextScaleCTM (context, self.frame.size.width/self.shapeMutablePathWidth, self.frame.size.height/self.shapeMutablePathHeight); 
CGContextAddPath(context, self.shapeMutablePath); 

...创建转化路径并使用它:

CGAffineTransform trn = CGAffineTransformMakeScale(self.bounds.size.width/self.shapeMutablePathWidth, self.bounds.size.height/self.shapeMutablePathHeight); 
CGPathRef transformedPath = CGPathCreateCopyByTransformingPath(self.shapeMutablePath, &trn); 
CGContextAddPath(context, transformedPath); 
CGPathRelease(transformedPath); 

这将填补和中风相同(缩放)区域,但转换不会影响笔划宽度。

B.t.w.您通常会使用边界,而不是框架的大小来计算比例。