iphone - 使用 CGContextScaleCTM 时避免拉伸(stretch)行程

标签 iphone ios cocoa-touch core-graphics

我正在 drawRect 中绘制一个形状,该形状存储在 CGMutablePathRef (shapeMutablePath) 中。每次调用drawRect时,形状都会被拉伸(stretch)以适合屏幕,并在其周围带有笔划边框。我想知道,如何在不拉伸(stretch)的情况下绘制笔划边框?即拉伸(stretch) shapeMutablePath,然后在其周围绘制笔画边框,以便每次绘制时宽度都相同?我尝试更改比例的顺序以及添加和绘制路径,但无济于事。

- (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);    
}

最佳答案

不缩放 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);

这将填充并描边相同的(缩放的)区域,但变换不会影响描边宽度。

顺便说一句。您通常会使用边界而不是框架的大小来计算比例。

关于iphone - 使用 CGContextScaleCTM 时避免拉伸(stretch)行程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12108804/

相关文章:

iphone - Obj-C,僵尸内存泄漏,我看不到?

iphone - 使用 kCATransitionPush 向上滑动 UIView

ios - MPMediaQuery.artistsQuery() 以组形式显示

ios - 自动滚动到 Swift 中 PFQueryTableViewController 中的下一个单元格

objective-c - iOS 多人游戏

iphone - UITableView 委托(delegate)是如何工作的?

iOS:如何在tableview中响应显示参数?

iphone - 使 UIView 的特定角变圆

iphone - 如何使用 xib 创建 UIView 子类?

ios - 如何隐藏工具栏中的反馈按钮?