Learn-iOS-Swift-by-Examples/Badger/Common/CAAnimation+SceneName.swift

37 lines
1.1 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
Copyright (C) 2016 Apple Inc. All Rights Reserved.
See LICENSE.txt for this samples licensing information
Abstract:
An extension on CAAnimation used to load animations from an SCNScene.
*/
import SceneKit
// MARK: Core Animation
extension CAAnimation {
class func animation(withSceneName name: String) -> CAAnimation {
guard let scene = SCNScene(named: name) else {
fatalError("Failed to find scene with name \(name).")
}
var animation: CAAnimation?
scene.rootNode.enumerateChildNodes { (child, stop) in
guard let firstKey = child.animationKeys.first else { return }
animation = child.animation(forKey: firstKey)
stop.initialize(to: true)
}
guard let foundAnimation = animation else {
fatalError("Failed to find animation named \(name).")
}
foundAnimation.fadeInDuration = 0.3
foundAnimation.fadeOutDuration = 0.3
foundAnimation.repeatCount = 1
return foundAnimation
}
}