Getting a problem in the CGPath scale

I draw a polygon using CGPath and adding to CAShapeLayer . I want to scale my CGPath when the user clicks on it. I know how to scale CGPath . But when I click on my CGPath , my CGPath drawing CGPath far from the center, while I draw a polygon in the center.

 CGAffineTransform scaleTransform = CGAffineTransformMakeScale(scaleFactor, scaleFactor); CGPathRef oldPath = polygonLayer.path; CGPathRef scaledPath = CGPathCreateCopyByTransformingPath(oldPath, &scaleTransform); polygonLayer.path = scaledPath; 
+3
source share
1 answer

The problem is that you are used to the UIView transform, which is done from the center of the view.
CGPath conversion is done at points (imagine CGPointZero as the center of the path).
My solution: translate to CGPointZero, scale, and then return to the original coordinates.

 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; } 
+3
source

All Articles