我正在使用 CGPath
绘制多边形并添加到 CAShapeLayer
。我想在用户单击它时缩放我的 CGPath
。我知道如何缩放 CGPath
。但是当我单击我的 CGPath
时,我的 CGPath
绘制远离中心,而我在中心绘制多边形。
CGAffineTransform scaleTransform = CGAffineTransformMakeScale(scaleFactor, scaleFactor);
CGPathRef oldPath = polygonLayer.path;
CGPathRef scaledPath = CGPathCreateCopyByTransformingPath(oldPath, &scaleTransform);
polygonLayer.path = scaledPath;
最佳答案
问题是你习惯了 UIView 转换,它是从 View 的中心完成的。
CGPath 转换是在点上完成的(想象 CGPointZero
作为路径的中心)。
我的解决方案:转换为 CGPointZero 、缩放,然后返回到原始坐标。
CGPathRef CGPath_NGCreateCopyByScalingPathAroundCentre(CGPathRef path,
const float scale)
{
CGRect bounding = CGPathGetPathBoundingBox(path);
CGPoint pathCenterPoint = CGPointMake(CGRectGetMidX(bounding), CGRectGetMidY(bounding));
CGAffineTransform translateAndScale = CGAffineTransformTranslate( CGAffineTransformMakeScale(scale, scale), - pathCenterPoint.x, -pathCenterPoint.y) ;
CGAffineTransform translateBack = CGAffineTransformMakeTranslation(pathCenterPoint.x, pathCenterPoint.y);
CGPathRef centeredAndScaled = CGPathCreateCopyByTransformingPath(path, &translateAndScale);
CGPathRef translatedPathRef = CGPathCreateCopyByTransformingPath(centeredAndScaled, &translateBack);
CGPathRelease(centeredAndScaled);
return translatedPathRef;
}
关于ios - 在 CGPath Scale 中遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18436587/