ios – 缩放CGPath以适应UIVI

ios – 缩放CGPath以适应UIVI,第1张

概述我需要知道如何可以缩小或升高CGPath,以便它可以适应UIView? 将背景颜色设置为黄色,以清楚地看到视图 self.frame = CGRectMake(0, 0, 181, 154);self.backgroundColor = [UIColor yellowColor]; 绘制CAShapeLayer CAShapeLayer *shape = [CAShapeLayer layer] 我需要知道如何可以缩小或升高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 createdCGRect 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 firstscaletransform = CGAffinetransformScale(scaletransform,scaleFactor,scaleFactor);// Then translate the path to the upper left cornerscaletransform = 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 codeCGPathref scaledpath = CGPathCreatecopyBytransformingPath(shapeLayer.path,&scaletransform);// Create a new shape layer and assign the new pathCAShapeLayer *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所遇到的程序开发问题。

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

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1096706.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存