{"id":2454,"date":"2013-06-01T19:51:48","date_gmt":"2013-06-01T18:51:48","guid":{"rendered":"http:\/\/t-machine.org\/?p=2454"},"modified":"2013-06-01T19:51:48","modified_gmt":"2013-06-01T18:51:48","slug":"svgkit-2013-recipes-part-2","status":"publish","type":"post","link":"http:\/\/new.t-machine.org\/index.php\/2013\/06\/01\/svgkit-2013-recipes-part-2\/","title":{"rendered":"SVGKit 2013 &#8211; Recipes (part 2)"},"content":{"rendered":"<p>(see also <a href=\"http:\/\/t-machine.org\/index.php\/2013\/01\/02\/svgkit-2013-recipes\/\">the first set of SVGKit recipes<\/a>)<\/p>\n<h3>Change the color of a SVG element like a Path or Circle after the user click on it<\/h3>\n<p>[objc]<br \/>\nCALayer* layerUserClickedOn = &#8230;<\/p>\n<p>if( [layerUserClickedOn isKindOfClass:[CAShapeLayer class]] ) \/\/ should ALWAYS be true, but just in case you write your code wrong&#8230;<br \/>\n{<br \/>\n   CAShapeLayer* shapeLayer = (CAShapeLayer*) layerUserClickedOn;<br \/>\n   shapeLayer.fillColor = [UIColor redColor].CGColor;<br \/>\n}<br \/>\n[\/objc]<\/p>\n<h3>Find the CALayer that was generated from a particular SVGElement<\/h3>\n<p>NB: Apple\/ObjectiveC uses &#8220;id&#8221; as a keyword, so we had to rename the SVG name &#8220;id&#8221; to &#8220;identifier&#8221;.<\/p>\n<p>If your SVG file is legal, and has a unique &#8220;id&#8221; attribute for every SVGElement, then you can find the CALayer directly from the SVGElement, using the &#8220;id&#8221; attribute. If your SVG file has no &#8220;id&#8221; attributes, or the SVGElement you&#8217;re searching has no &#8220;id&#8221; element, then you&#8217;ll have to do more work &#8211; or fix the SVG file (edit it, add an &#8220;id&#8221; attribute to the SVGElement that matters, save it &#8230; then use this recipe)!<\/p>\n<p>[objc]<br \/>\nSVGKImage* svgImage = &#8230;<br \/>\nSVGElement* element = &#8230; \/\/ see previous SVGKit recipes<br \/>\nCALayer* layer = [svgImage layerWithIdentifier:element.identifier];<br \/>\n&#8230; \/\/ do something with it&#8230;<br \/>\n[\/objc]<\/p>\n<h3>Clone part of the image, and move it around \/ change it<\/h3>\n<p>First of all, find the part of the SVG you want to clone, e.g. using &#8220;Search an SVG file for particular tags \/ nodes \/ elements&#8221; from <a href=\"http:\/\/t-machine.org\/index.php\/2013\/01\/02\/svgkit-2013-recipes\/\">the first set of SVGKit recipes<\/a>.<\/p>\n<p>That gives you an (SVGElement*). Use the previous recipe to get the CALayer. Finally, clone it and use it:<\/p>\n<p>[objc]<br \/>\n#import &quot;CALayer+RecursiveClone.h&quot; \/\/ requires SVGKit version 1.1.0 or later<\/p>\n<p>&#8230;<br \/>\n-(void) someMethod<br \/>\n{<br \/>\n   SVGKImage* svgImage = &#8230;<br \/>\n   SVGElement* elementToClone = &#8230; \/\/ see previous SVGKit recipes<br \/>\n   CALayer* layerToClone = &#8230; \/\/ see recipe above<\/p>\n<p>   CALayer* newLayer = [layerToClone cloneRecursively];<br \/>\n   newLayer.frame = layerToClone.bounds; \/\/ reset it to be positioned at (0,0)<\/p>\n<p>   \/\/ if you want the clone to start in same position as original<br \/>\n   \/\/ add this code<br \/>\n   if( calculatePositionOfOriginalLayer )<br \/>\n   {<br \/>\n      CGFloat xOffsetOriginal = 0.0f;<br \/>\n      CGFloat yOffsetOriginal = 0.0f;<br \/>\n      CALayer* parentLayer = layerToClone;<br \/>\n      while( nil != (parentLayer = parentLayer.superlayer) )<br \/>\n      {<br \/>\n         xOffsetOriginal += parentLayer.frame.origin.x;<br \/>\n         yOffsetOriginal += parentLayer.frame.origin.y;<br \/>\n      }<br \/>\n      CGPoint positionOfOriginalLayer = CGPointApplyTransform( layerToClone.position, CGAffineMakeTranslation( xOffsetOriginal, yOffsetOriginal ) );<br \/>\n   }<br \/>\n}<br \/>\n[\/objc]<\/p>\n<h3>Edit the polygons \/ lines \/ shapes after loading<\/h3>\n<p>Every &#8220;shape&#8221; is stored as some form of Apple CGPath object.<\/p>\n<p>[objc]<br \/>\nCALayer* layerInSVG = &#8230; \/\/ use a previous recipe to get the layer from the SVG element<br \/>\nCAShapeLayer* shapeLayer = (CAShapeLayer*) layerInSVG; \/\/ if you chose wrong, this will error at runtime<br \/>\nCGPath pathToEdit = shapeLayer.path;<\/p>\n<p>&#8230; \/\/ edit the path, and create a replacement path<\/p>\n<p>shapeLayer.path = myEditedPath;<br \/>\n[\/objc]<\/p>\n<p>Apple doesn&#8217;t provide an API to directly edit CGPath objects. They have a C API that let&#8217;s you create a callback function that turns the CGPath into a list of draw-calls, that you can then work with. But it takes quite a bit of code to read this, and then convert it back into a CGPath that Apple can draw.<\/p>\n<p>So far, no-one has open-sourced their code for this (I&#8217;ve got a private version I wrote for a previous project, but it&#8217;s thousands of lines of code). Either petition Apple to add the missing APIs, or find someone to write it for you \/ license it to you, or write it yourself.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>(see also the first set of SVGKit recipes) Change the color of a SVG element like a Path or Circle after the user click on it [objc] CALayer* layerUserClickedOn = &#8230; if( [layerUserClickedOn isKindOfClass:[CAShapeLayer class]] ) \/\/ should ALWAYS be true, but just in case you write your code wrong&#8230; { CAShapeLayer* shapeLayer = (CAShapeLayer*) [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[],"_links":{"self":[{"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/posts\/2454"}],"collection":[{"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/comments?post=2454"}],"version-history":[{"count":4,"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/posts\/2454\/revisions"}],"predecessor-version":[{"id":2458,"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/posts\/2454\/revisions\/2458"}],"wp:attachment":[{"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/media?parent=2454"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/categories?post=2454"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/tags?post=2454"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}