ios - 在 CGPath Scale 中遇到问题

标签 ios cgpath cashapelayer

我正在使用 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/

相关文章:

ios - 基于有序的对多关系对描述符进行排序

iphone - 每 n 分钟在标签中显示 NSString

swift 3(SpriteKit): Change the properties of each line in CGPath

ios - 如何在 Swift 中向 UIImageView 添加自定义形状?

ios - 使用移动框架为 CAShapeLayer 设置动画

ios,给定 X/Y 坐标如何获取底层 View

ios - AEXMLDocument loadXMLData() 在 Swift 中不起作用

swift - 部分 UIView 的屏幕截图被屏蔽为 UIBezierPath/CGPath

ios - 使用 UIImageView 在 UIView 中切割透明孔

ios - 在 UIView Swift 中居中 CAShapeLayer