iOS   发布时间:2022-03-31  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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

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

大佬总结

以上是大佬教程为你收集整理的ios – 缩放CGPath以适应UIVI全部内容,希望文章能够帮你解决ios – 缩放CGPath以适应UIVI所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。