39408

Getting issue in CGPath Scale

Question:

I am drawing polygon using CGPath and adding to CAShapeLayer.I want to scale my CGPath when user click on it. I know how to scale CGPath. But when i click my CGPath, my CGPath drawing far from centre while i am drawing polygon in centre.

CGAffineTransform scaleTransform = CGAffineTransformMakeScale(scaleFactor, scaleFactor); CGPathRef oldPath = polygonLayer.path; CGPathRef scaledPath = CGPathCreateCopyByTransformingPath(oldPath, &scaleTransform); polygonLayer.path = scaledPath;

Answer1:

The problem is that you're used to UIView transformation, which is done from the center of the view.<br /> CGPath transformation is done on the points (imagine CGPointZero as the center of the path).<br /> My solution: translate to CGPointZero , scale , and then back to your 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; }

Recommend

  • iOS SpriteKit How to make stroke color transparent?
  • Simplest way to use a variable in the URL in servlets
  • Restrict an object to stay within another object
  • Can I build both 32-bit and 64-bit architectures of a library on Mac OS?
  • How to make a npm command, that executes two commands in the console (one after another)?
  • including Python.h in C++ file CDT
  • C# Remove URL from String
  • Symfony2 plaintext users don't work
  • Error “Reflect.defineMetadata” while trying to load a transient web worker
  • How could I prohibit anonymous access to my NodeRed UI Dashboard on IBM Cloud(Bluemix)?
  • PostgreSQL 9.x - pg_read_binary_file & inserting files into bytea
  • Programmatically Update Linked Named Range of excel object in MS Word (2007)
  • LNK1104: cannot open file 'kernel32.lib'
  • Validation fired but Red Border does not appear with User Control in Silverlight 4
  • What does Main.1 output file do in XCode?
  • d3.js: why is d3.geo.path() giving NaN?
  • How to set current CultureUI via XAML binding
  • Hiding Directories Programatically in C#
  • File not found error python
  • Why are “sc.addFile” and “spark-submit --files” not distributing a local file to all workers?
  • My first C# WPF ValidationRule is not firing
  • Raphael.js function getBBox give back NAN/NAN/NAN in IE8
  • node.js POST request fails
  • XMLBeans Duplicate Class and Class Not Found Errors
  • How to rotate the dial in a gauge chart? Using python plotly
  • How to draw a line dynamically in android [duplicate]
  • New Firebase failed: First argument must be a valid firebase URL and the path can't contain “.”
  • php script is parsing content from RTE (tt_news) but internal links are not appearing as speaking ur
  • Jquery popup on mouse over of calendar control
  • WPF Visiblity Binding to Boolean Expression with multiple Variables
  • how to upload multiple files in c# windows application
  • MS Access - How to change the linked table path by amend the table
  • JBoss External Properties Files in Classpath
  • Connect .sks to skscene.h
  • Why does access(2) check for real and not effective UID?
  • Checking free space on FTP server
  • script to move all files from one location to another location
  • ILMerge & Keep Assembly Name
  • Symfony2: How to get request parameter
  • Run Powershell script from inside other Powershell script with dynamic redirection to file