

- Change color of paintcode dynamically swift how to#
- Change color of paintcode dynamically swift pdf#
- Change color of paintcode dynamically swift archive#
- Change color of paintcode dynamically swift code#
Change color of paintcode dynamically swift code#
The code above is fairly straightforward.

To make things nicer, we could write an AnimationArchive class, like this one: final class AnimationArchive ĪnimationLayer. We’re just doing the reverse of what we did before: loading the data, unarchiving it as a dictionary with String keys and Any values, and grabbing the rootLayer key, which will be of type CALayer.

Let rootLayer = caar as! CALayer // Do something with rootLayer, such as add it to a view unarchiveTopLevelObjectWithData(data) as! Force-unwrapping for demo purposes, "!" is evil, don't use it let caar = try! NSKeyedUnarchiver. To load and display this file programmatically, you have to do something like this: let data: Data = // load file from disk If you open this file using my CAARPlayer app, this is the result: Let url = URL(fileURLWithPath: "\(path) /redSquare.caar") Write test CAAR file to the Documents directory let path = NSSearchPathForDirectoriesInDomains(. archivedData(withRootObject: caar, requiringSecureCoding: false) Use NSKeyedArchiver to "freeze-dry" the layer tree let data = try NSKeyedArchiver. lineWidth = 2 // Create dictionary required to comply with the CAAR format let caar = path = CGPath(roundedRect: rect, cornerWidth: 5, cornerHeight: 5, transform: nil)
Change color of paintcode dynamically swift archive#
Here’s a simple way to create a Core Animation Archive programmatically: // Create a simple square layer let layer = CAShapeLayer() CAAR files usually consist of a dictionary as the root object, this dictionary has a key called rootLayer, with its value being, you guessed it, the root layer of the archive that should be read by the application and drawn on screen.
Change color of paintcode dynamically swift how to#
So all you need to know to create yourself a Core Animation Archive is how to use NSKeyedArchiver and which format the archive should be in. Storyboards and XIBs work this way at runtime: they’re kinda like freeze-dried objects. If you’re not familiar with NSKeyedArchiver, all you need to know is that it’s a very old API (being old doesn’t mean bad or deprecated, it’s just old) that takes objects from memory and encodes them in a way that can be stored on disk and then transformed back into objects later. These files are actually fairly straightforward: they consist of a Core Animation layer tree which is archived using NSKeyedArchiver, resulting in a “frozen” layer tree you can store on disk and then load again at runtime. There’s this thing called a “Core Animation Archive”, you can find one if you look into Apple’s apps and frameworks, usually represented by a file with the extension. The one I’m going to propose today is not used a lot by third-party apps, but it is used a lot by Apple’s apps and system components. If you need animated vector graphics that can be scaled and possibly transformed in other ways at runtime, you’re better off by using another technique. If you don’t have to support a very large variety of screen shapes and dimensions, a simple video should work. That’s a valid approach depending on what you’re doing. One way to represent animations as assets is to encode them as video files and play them back using something like AVFoundation. I’m not a fan of that approach, and the same thing goes for animations. Some people like to draw everything in code, even using apps such as PaintCode to generate it for them.
Change color of paintcode dynamically swift pdf#
If you have a button and that button has an icon, that icon should be an asset, either a set of PNGs or a PDF if you’d like to keep the vector data. I’ve always been in favor of letting assets be assets. What I do believe in is that complex animations, especially ones that are not very dependent on dynamic data that’s only known at runtime, should be assets. I don’t mean that every single animation in an app must be represented by an asset and can’t be done programmatically, since that would be dumb. * Color 0.808, 0.808, 0.808 = gray (starting color in this var redColor: CGFloat = 0.I should start by explaining what I mean by “animations are assets”. * Notice - supported range for colors and alpha: 0 to 1.

The generated color is then assigned to the Stroke, Fill, whatever, via that expression. Create an expression that takes red, green, blue, alpha ( external parameters in PaintCode) and generates a color ( makeColor function in PaintCode).
