-
Notifications
You must be signed in to change notification settings - Fork 0
/
StatefulImageButtonVC.swift
81 lines (62 loc) · 2.36 KB
/
StatefulImageButtonVC.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
//
// StatefulImageButtonVC.swift
// ASDisplayNodes+Style
//
// Created by Adam Tait on 2/21/18.
// Copyright © 2018 Sisterical Inc. All rights reserved.
//
import Foundation
import AsyncDisplayKit
class StatefulImageButtonVC : ImageButtonVC
{
let selectedImageNode : ASImageNode
// initializers
init(style : Style,
selectedStyle : Style)
{
let imageNode = ImageButtonVC.newImageNode(style: style)
self.selectedImageNode = ImageButtonVC.newImageNode(style: selectedStyle)
super.init(node: ASControlNode(), style: style)
Layout.mutate(node : self.node,
imageNode : imageNode,
style : style,
selectedImageNode : self.selectedImageNode,
selectedStyle : selectedStyle,
selected : selected)
_ = self.selected.addObserver(selectedChanged)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// Observers
func selectedChanged(_: Observable)
{
self.node.transitionLayout(withAnimation: true,
shouldMeasureAsync: true,
measurementCompletion: nil)
}
}
// layout
extension StatefulImageButtonVC
{
enum Layout
{
static func mutate(node n : ASControlNode,
imageNode : ASImageNode,
style : Style,
selectedImageNode : ASImageNode,
selectedStyle : Style,
selected : MutableProperty<Bool>)
{
n.automaticallyManagesSubnodes = true
n.layoutSpecBlock = { node, size in
let activeNode = selected.get()! ? selectedImageNode : imageNode
let activeStyle = selected.get()! ? selectedStyle : style
n.styleset.set(activeStyle)
let insets = activeStyle[.insets] as! UIEdgeInsets
return ASInsetLayoutSpec(insets: insets,
child: activeNode)
}
}
}
}