Created
October 23, 2018 09:20
-
-
Save mihaelamj/48951d7aa1865d9962426b73f3e10f2c to your computer and use it in GitHub Desktop.
caar UIVIew extension for loading .caar files. great with Kite Compositor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import UIKit | |
| //Example from: | |
| //https://github.com/kitecomp/archive-example | |
| //wrapped into a small UIView category | |
| //https://gist.github.com/mihaelamj/79878ba7b7881a12f1652c113adcf5d8 | |
| public extension UIView { | |
| public func loadCAARAnimation(named: String, scale : CGFloat = 1.0) -> CALayer? { | |
| // Path to the .caar file, should be in the boundle | |
| guard let caarURL = Bundle.main.url(forResource: named, withExtension: "caar") else { return nil } | |
| //Unarchive the NSKeyedArchive as a Dictionary | |
| guard let coreAnimationData = try? Data(contentsOf: caarURL) else { return nil } | |
| guard let rootObject = (try? NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(coreAnimationData)) as? [String: Any] else { return nil} | |
| // Pull out the 'rootLayer' object containing the archived CALayer | |
| guard let rootLayer = rootObject["rootLayer"] as? CALayer else { return nil} | |
| //Un-flip the layer for iOS's coordinate system | |
| rootLayer.isGeometryFlipped = false | |
| // Start the rootLayer's timeline at the current time | |
| rootLayer.beginTime = CACurrentMediaTime() | |
| self.layer.addSublayer(rootLayer) | |
| //apply scale | |
| if (scale != 1.0) { | |
| rootLayer.transform = CATransform3DMakeScale(scale, scale, 1) | |
| } | |
| // rootLayer.frame | |
| return rootLayer | |
| } | |
| } | |
| /* | |
| usage: | |
| let anyLayer = av.loadCAARAnimation(named: "loading", scale: 0.2) | |
| if let aLayer = anyLayer { | |
| av.heightAnchor.constraint(equalToConstant: aLayer.frame.size.height).isActive = true | |
| av.widthAnchor.constraint(equalToConstant: aLayer.frame.size.width).isActive = true | |
| } | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment