我使用CGPath
绘制多边形并添加到CAShapeLayer
。我希望在用户点击它时缩放CGPath
。我知道如何扩展CGPath
。但是当我点击我的CGPath
时,我的CGPath
绘图远离中心,而我在中心绘制多边形。
CGAffineTransform scaleTransform = CGAffineTransformMakeScale(scaleFactor, scaleFactor);
CGPathRef oldPath = polygonLayer.path;
CGPathRef scaledPath = CGPathCreateCopyByTransformingPath(oldPath, &scaleTransform);
polygonLayer.path = scaledPath;
答案 0 :(得分:4)
问题在于你已经习惯了UIView转换,这是从视图的中心完成的
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;
}