Comments (5)
If you're curious, here's my JsonDataExtractor class:
import UIKit
class JsonDataExtractor: KFVector {
func loadVectorFromDisk(assetName:String) throws -> KFVector {
let filePath : String = Bundle(for: type(of: self)).path(forResource: assetName, ofType: "json")!
let data : Data = try String(contentsOfFile: filePath).data(using: .utf8)!
let vectorDictionary : Dictionary = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as! [String:Any]
return KFVectorFromDictionary(vectorDictionary)
}
}
from keyframes.
Hi @markjeschke,
I would do pretty much the same as you did. Maybe encapsulate do catch
into your extractor too?
or have a func keyframesLayerFromAsset(assetName:String) -> KFVectorLayer
if those layers share the same setup such as frame
, repeatCount
etc.
All steps are required though.
from keyframes.
Thanks for your help. How would you encapsulate the do
catch
into the extractor? It would be great if that could also be included in the keyFramesLayerFromAsset
function that you proposed.
For instance, if I added a new animation via a UIButton action, couldn't a single method call could create an object that handles the KFVector, the JSON File path, and the KFVectorLayers, all in one declaration with tuples or instanceType arguments? I'm sorry if this is trivial.
Thanks in advance,
Mark
from keyframes.
I finally came up with a solution, based upon @LazyChild's recommendations. I still need to optimize the code's functionality into separate classes or extensions and remove the print statements. But, I'm hoping that it will help others. Any advice on making the code more efficient is welcome. Here's the ViewController:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let shortSide = min(self.view.bounds.width, self.view.bounds.height)
let longSide = max(self.view.bounds.width, self.view.bounds.height)
// ===== Top Animation
let topKeyframeVector = keyframesLayerFromAsset(assetName: "sample_logo",
repeatCount: 2,
x: shortSide / 4 + 25,
y: 20,
width: 100,
height: 100)
// Add the animation layer to the view.
self.view.layer.addSublayer(topKeyframeVector)
topKeyframeVector.startAnimation()
// ===== Middle Animation
let sVector = keyframesLayerFromAsset(assetName: "keyframes",
repeatCount: 3,
x: shortSide / 4,
y: longSide / 2 - shortSide / 4,
width: shortSide / 2,
height: shortSide / 2)
// Add the animation layer to the view.
self.view.layer.addSublayer(sVector)
sVector.startAnimation()
// ===== Bottom Animation
let bottomKeyframeVector = keyframesLayerFromAsset(assetName: "sample_logo",
repeatCount: 1,
x: shortSide / 4 + 20,
y: longSide - 140,
width: 120,
height: 120)
// Add the animation layer to the view.
self.view.layer.addSublayer(bottomKeyframeVector)
bottomKeyframeVector.startAnimation()
}
// ===== Extract JSON data
func loadVectorFromDisk(assetName:String) throws -> KFVector {
let filePath : String = Bundle(for: type(of: self)).path(forResource: assetName, ofType: "json")!
let data : Data = try String(contentsOfFile: filePath).data(using: .utf8)!
let vectorDictionary : Dictionary = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as! [String:Any]
return KFVectorFromDictionary(vectorDictionary)
}
// ===== Call the animation layer and pass in parameters
func keyframesLayerFromAsset(assetName: String,
repeatCount: Float,
x: CGFloat,
y: CGFloat,
width: CGFloat,
height: CGFloat) -> KFVectorLayer {
var myVector : KFVector!
do {
myVector = try loadVectorFromDisk(assetName: assetName)
print("assetName: \(assetName)")
} catch {
print("Vector file could not be loaded, aborting")
}
let myVectorLayer = KFVectorLayer()
myVectorLayer.frame = CGRect(x: x, y: y, width: width, height: height)
// For some reason setting a repeatCount = 0 doesn't work on its own.
if repeatCount != 0 {
myVectorLayer.repeatCount = repeatCount
}
myVectorLayer.faceModel = myVector!
print("repeatCount: \(repeatCount)\n-----")
return myVectorLayer
}
}
from keyframes.
@lozzle Please feel free to close this issue if it shouldn't be in the Issues section. :)
from keyframes.
Related Issues (20)
- Show different in ios and android HOT 2
- Is there support of macOS now? HOT 3
- [Android] - Support size change? HOT 2
- Pod name is `keyframes` instead of `Keyframes`
- Any updates on the new release date? HOT 1
- [iOS] animationDidStopBlock called twice HOT 5
- cocoapods release HOT 1
- Illegar argument for frame_rate HOT 1
- Path trimming and bitmap support not working HOT 2
- List effects after effect available in keyframes HOT 1
- Publish updated keyframes-cli to npm?
- From AE: Only serialize images used in active comp, not ALL of them in the project
- Suggestion: add Keyframes Player information to readme
- keyframes-cli: Exported files should include AEP name as well as comp name
- Carthage Support Broken On Case-Sensitive FS
- Values in timing_curves outside of [0,1]
- can't import HOT 1
- Suggestion: remove "buildToolsVersion" and its usages
- This Folder?
- KeyFrames not working in custom view image
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from keyframes.