-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathTransitionLayoutViewController.swift
166 lines (129 loc) · 3.81 KB
/
TransitionLayoutViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//
// TransitionLayoutViewController.swift
// Demo
//
// Created by Muukii on 2021/05/15.
// Copyright © 2021 muukii. All rights reserved.
//
import Foundation
import TextureSwiftSupport
import GlossButtonNode
import TypedTextAttributes
/**
Does not work
https://github.com/TextureGroup/Texture/issues/1995
*/
final class TransitionLayoutViewController: DisplayNodeViewController {
private let contentNode = ContentNode()
override func layoutSpecThatFits(_ constrainedSize: ASSizeRange) -> ASLayoutSpec {
LayoutSpec {
VStackLayout {
contentNode
HStackLayout {
node._makeNode {
GlossButtonNode.simpleButton(title: "Large") { [unowned self] in
contentNode.large()
}
}
node._makeNode {
GlossButtonNode.simpleButton(title: "Small") { [unowned self] in
contentNode.small()
}
}
}
}
.padding(node.capturedSafeAreaInsets)
}
}
private final class ContentNode: NamedDisplayNodeBase {
private enum LayoutMode {
case large
case small
}
private let imageNode = Mocks.ImageNode()
struct SmallComponents {
let titleNode = ASTextNode()
let artistNameNode = ASTextNode()
}
struct LargeComponents {
let titleNode = ASTextNode()
let artistNameNode = ASTextNode()
}
private let smallComponents = SmallComponents()
private let largeComponents = LargeComponents()
private var layoutMode: LayoutMode = .large
private var animateBlock: () -> Void = {}
override init() {
super.init()
addSubnode(imageNode)
addSubnode(smallComponents.titleNode)
addSubnode(smallComponents.artistNameNode)
addSubnode(largeComponents.titleNode)
addSubnode(largeComponents.artistNameNode)
// automaticallyManagesSubnodes = true
smallComponents.titleNode.attributedText = "Mosaik".styled {
$0.font(.preferredFont(forTextStyle: .headline))
}
smallComponents.artistNameNode.attributedText = "Township Rebelion".styled {
$0.font(.preferredFont(forTextStyle: .headline))
}
largeComponents.titleNode.attributedText = "Mosaik".styled {
$0.font(.preferredFont(forTextStyle: .headline))
}
largeComponents.artistNameNode.attributedText = "Township Rebelion".styled {
$0.font(.preferredFont(forTextStyle: .headline))
}
}
override func didLoad() {
super.didLoad()
large()
}
override func layoutSpecThatFits(_ constrainedSize: ASSizeRange) -> ASLayoutSpec {
LayoutSpec {
switch layoutMode {
case .small:
HStackLayout {
imageNode
.aspectRatio(1)
.width(55)
SpacerLayout(minLength: 16, flexGrow: 0)
VStackLayout(alignItems: .start) {
smallComponents.titleNode
smallComponents.artistNameNode
}
}
case .large:
VStackLayout {
imageNode
.aspectRatio(1)
VStackLayout(alignItems: .start) {
largeComponents.titleNode
largeComponents.artistNameNode
}
}
}
}
}
func large() {
layoutMode = .large
UIViewPropertyAnimator(duration: 2, dampingRatio: 0.9) {
self.setNeedsLayout()
self.layoutIfNeeded()
}
.startAnimation()
}
func small() {
layoutMode = .small
UIViewPropertyAnimator(duration: 2, dampingRatio: 0.9) {
self.setNeedsLayout()
self.layoutIfNeeded()
}
.startAnimation()
}
// override func animateLayoutTransition(_ context: ASContextTransitioning) {
// print("xxx", context)
// animateBlock()
// animateBlock = {}
// }
}
}