Layout Transition API는 Texture에서 제공하는 모든 컴포넌트들에 대해서 애니메이션을 쉽게 만들 수 있도록 설계되었습니다. 심지어 전체 뷰 집합을 완전히 다른 뷰 집합으로 변형 할 수도 있습니다.
1. animateLayoutTransition을 override합니다.
class TestNode: ASDisplayNode {
// ...
override func animateLayoutTransition(_ context: ASContextTransitioning) {
// 1. transition되기 이전의 frame 값을 가져옵니다.
let beforeFrame = context.initialFrame(for: TARGET_NODE)
// 2. transition되고 난 이후의 frame 값을 가져옵니다.
let afterFrame = context.finalFrame(for: TARGET_NODE)
self.TARGET_NODE.frame = beforeFrame
UIView.animate(withDuration: 2.0,
delay: 0.5,
options: .curveEaseOut,
animations: {
self.TARGET_NODE.frame = afterFrame
}, completion: { isCompleted in
// 3. ASContextTransitioning을 완료 시킵니다.
context.completeTransition(isCompleted)
})
}
// ...
}
2. 에니메이션 처리를 위해 Transition Layout API를 호출합니다.
self.transitionLayout(withAnimation: Bool,
shouldMeasureAsync: Bool,
measurementCompletion: () -> Void)
class ProgressBarNode: ASDisplayNode {
lazy var progressEngageNode: ASDisplayNode = {
let node = ASDisplayNode()
node.backgroundColor = .red
return node
}()
private var ratio: CGFloat = 0.0
override init() {
super.init()
self.automaticallyManagesSubnodes = true
self.backgroundColor = .lightGray
self.style.height = .init(unit: .points, value: 50.0)
}
override func animateLayoutTransition(_ context: ASContextTransitioning) {
// 1. transition되기 이전의 frame 값을 가져옵니다.
let beforeFrame = context.initialFrame(for: progressEngageNode)
// 2. transition되고 난 이후의 frame 값을 가져옵니다.
let afterFrame = context.finalFrame(for: progressEngageNode)
progressEngageNode.frame = beforeFrame
progressEngageNode.alpha = 0.0
UIView.animate(withDuration: 0.5,
delay: 0.0,
options: .curveEaseOut,
animations: {
self.progressEngageNode.alpha = 1.0
self.progressEngageNode.frame = afterFrame
}, completion: { isCompleted in
// 3. ASContextTransitioning을 완료 시킵니다.
context.completeTransition(isCompleted)
})
}
public func setRatio(_ ratio: CGFloat) {
self.ratio = ratio
self.transitionLayout(withAnimation: true,
shouldMeasureAsync: true,
measurementCompletion: nil)
}
override func layoutSpecThatFits(_ constrainedSize: ASSizeRange) -> ASLayoutSpec {
progressEngageNode.style.flexBasis = .init(unit: .fraction, value: ratio)
let spaceLayout = ASLayoutSpec()
spaceLayout.style.flexBasis = .init(unit: .fraction, value: 1.0 - ratio)
return ASStackLayoutSpec(direction: .horizontal,
spacing: 0.0,
justifyContent: .start,
alignItems: .stretch,
children: [progressEngageNode, spaceLayout])
}
}