微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ios – 缩放CGPath以适应UIVI

我需要知道如何可以缩小或升高CGPath,以便它可以适应UIView?

将背景颜色设置为黄色,以清楚地看到视图

self.frame = CGRectMake(0,181,154);
self.backgroundColor = [UIColor yellowColor];

绘制CAShapeLayer

CAShapeLayer *shape = [CAShapeLayer layer];
shape.path = aPath;

shape.strokeColor = [[UIColor blueColor] CGColor];
shape.linewidth = 5;
shape.fillColor = [[UIColor clearColor] CGColor];

[self.layer addSublayer:shape];

然后我得到这个:

我想要的是:

谢谢开发人员:)

解决方法

我假设你想完全填充(不扭曲)形状,以适应你的观点.我也假设你已经有一个具有路径的形状层,因为问题被标记为CAShapeLayer.

在这种情况下,您将获得形状图层路径的边界框,并计算适用于路径的适当比例因子.

根据形状的长宽比和它应该适合的视图,它将是确定比例因子的宽度或高度.

一旦你有了比例因子,你将创建一个应用于路径的转换.由于路径可能无法从(0,0)开始,您还将转换中的路径(移动)到边界框起点.

如果您想要将新路径置于视图的中心位置,则需要计算出移动的路径.如果您不需要该代码,您可以注释掉该代码,但我将其包含在其他人阅读此答案中.

最后,你有一个新的路径,所以你只需要创建一个新的形状图层,分配路径,适当地进行风格并将其添加到视图.

// I'm assuming that the view and original shape layer is already created
CGRect boundingBox = CGPathGetBoundingBox(shapeLayer.path);

CGFloat boundingBoxAspectRatio = CGRectGetWidth(boundingBox)/CGRectGetHeight(boundingBox);
CGFloat viewaspectRatio = CGRectGetWidth(viewToFitIn.frame)/CGRectGetHeight(viewToFitIn.frame);

CGFloat scaleFactor = 1.0;
if (boundingBoxAspectRatio > viewaspectRatio) {
    // Width is limiting factor
    scaleFactor = CGRectGetWidth(viewToFitIn.frame)/CGRectGetWidth(boundingBox);
} else {
    // Height is limiting factor
    scaleFactor = CGRectGetHeight(viewToFitIn.frame)/CGRectGetHeight(boundingBox);
}


// Scaling the path ...
CGAffineTransform scaleTransform = CGAffineTransformIdentity;
// Scale down the path first
scaleTransform = CGAffineTransformScale(scaleTransform,scaleFactor,scaleFactor);
// Then translate the path to the upper left corner
scaleTransform = CGAffineTransformTranslate(scaleTransform,-CGRectGetMinX(boundingBox),-CGRectGetMinY(boundingBox));

// If you want to be fancy you Could also center the path in the view
// i.e. if you don't want it to stick to the top.
// It is done by calculating the heigth and width difference and translating
// half the scaled value of that in both x and y (the scaled side will be 0)
CGSize scaledSize = CGSizeApplyAffineTransform(boundingBox.size,CGAffineTransformMakeScale(scaleFactor,scaleFactor));
CGSize centerOffset = CGSizeMake((CGRectGetWidth(viewToFitIn.frame)-scaledSize.width)/(scaleFactor*2.0),(CGRectGetHeight(viewToFitIn.frame)-scaledSize.height)/(scaleFactor*2.0));
scaleTransform = CGAffineTransformTranslate(scaleTransform,centerOffset.width,centerOffset.height);
// End of "center in view" transformation code

CGPathRef scaledpath = CGPathCreatecopyByTransformingPath(shapeLayer.path,&scaleTransform);

// Create a new shape layer and assign the new path
CAShapeLayer *scaledShapeLayer = [CAShapeLayer layer];
scaledShapeLayer.path = scaledpath;
scaledShapeLayer.fillColor = [UIColor blueColor].CGColor;
[viewToFitIn.layer addSublayer:scaledShapeLayer];

CGPathRelease(scaledpath); // release the copied path

在我的示例代码(和形状)中,它看起来像这样

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐