FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
AsyncDisplayKit/AsyncDisplayKit/ASDisplayNodeExtras.mm at master · SummerXZX/AsyncDisplayKit · GitHub
SummerXZX
/
AsyncDisplayKit
Public
forked from
facebookarchive/AsyncDisplayKit
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
AsyncDisplayKit
/
AsyncDisplayKit
/
ASDisplayNodeExtras.mm
Copy path
More file actions
More file actions
Latest commit
History
History
History
275 lines (228 loc) · 8.05 KB
Breadcrumbs
AsyncDisplayKit
/
AsyncDisplayKit
/
ASDisplayNodeExtras.mm
Copy path
File metadata and controls
275 lines (228 loc) · 8.05 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
//
//
ASDisplayNodeExtras.mm
//
AsyncDisplayKit
//
//
Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
//
This source code is licensed under the BSD-style license found in the
//
LICENSE file in the root directory of this source tree. An additional grant
//
of patent rights can be found in the PATENTS file in the same directory.
//
#
import
"
ASDisplayNodeExtras.h
"
#
import
"
ASDisplayNodeInternal.h
"
#
import
"
ASDisplayNode+FrameworkPrivate.h
"
#
import
<
queue
>
extern
ASInterfaceState
ASInterfaceStateForDisplayNode
(ASDisplayNode *displayNode, UIWindow *window)
{
ASDisplayNodeCAssert
(![displayNode
isLayerBacked
],
@"
displayNode must not be layer backed as it may have a nil window
"
);
if
(displayNode && [displayNode
supportsRangeManagedInterfaceState
]) {
//
Directly clear the visible bit if we are not in a window. This means that the interface state is,
//
if not already, about to be set to invisible as it is not possible for an element to be visible
//
while outside of a window.
ASInterfaceState interfaceState = displayNode.
interfaceState
;
return
(window ==
nil
? (interfaceState &= (~ASInterfaceStateVisible)) : interfaceState);
}
else
{
//
For not range managed nodes we might be on our own to try to guess if we're visible.
return
(window ==
nil
? ASInterfaceStateNone : (ASInterfaceStateVisible | ASInterfaceStateDisplay));
}
}
extern
ASDisplayNode *
ASLayerToDisplayNode
(
CALayer
*layer)
{
return
layer.
asyncdisplaykit_node
;
}
extern
ASDisplayNode *
ASViewToDisplayNode
(UIView *view)
{
return
view.
asyncdisplaykit_node
;
}
extern
void
ASDisplayNodePerformBlockOnEveryNode
(
CALayer
*layer, ASDisplayNode *node,
void
(^block)(ASDisplayNode *node))
{
if
(!node) {
ASDisplayNodeCAssertNotNil
(layer,
@"
Cannot recursively perform with nil node and nil layer
"
);
ASDisplayNodeCAssertMainThread
();
node =
ASLayerToDisplayNode
(layer);
}
if
(node) {
block
(node);
}
if
(!layer && [node
isNodeLoaded
] &&
ASDisplayNodeThreadIsMain
()) {
layer = node.
layer
;
}
if
(layer) {
//
/ NOTE: The docs say `sublayers` returns a copy, but it does not.
//
/ See: http://stackoverflow.com/questions/14854480/collection-calayerarray-0x1ed8faa0-was-mutated-while-being-enumerated
for
(
CALayer
*sublayer in [[layer
sublayers
]
copy
]) {
ASDisplayNodePerformBlockOnEveryNode
(sublayer,
nil
, block);
}
}
else
if
(node) {
for
(ASDisplayNode *subnode in [node
subnodes
]) {
ASDisplayNodePerformBlockOnEveryNode
(
nil
, subnode, block);
}
}
}
extern
void
ASDisplayNodePerformBlockOnEveryNodeBFS
(ASDisplayNode *node,
void
(^block)(ASDisplayNode *node))
{
//
Queue used to keep track of subnodes while traversing this layout in a BFS fashion.
std::queue<ASDisplayNode *> queue;
queue.
push
(node);
while
(!queue.
empty
()) {
node = queue.
front
();
queue.
pop
();
block
(node);
//
Add all subnodes to process in next step
for
(
int
i =
0
; i < node.
subnodes
.
count
; i++)
queue.
push
(node.
subnodes
[i]);
}
}
extern
void
ASDisplayNodePerformBlockOnEverySubnode
(ASDisplayNode *node,
void
(^block)(ASDisplayNode *node))
{
for
(ASDisplayNode *subnode in node.
subnodes
) {
ASDisplayNodePerformBlockOnEveryNode
(
nil
, subnode, block);
}
}
ASDisplayNode *
ASDisplayNodeFindFirstSupernode
(ASDisplayNode *node,
BOOL
(^block)(ASDisplayNode *node))
{
CALayer
*layer = node.
layer
;
while
(layer) {
node =
ASLayerToDisplayNode
(layer);
if
(
block
(node)) {
return
node;
}
layer = layer.
superlayer
;
}
return
nil
;
}
__kindof ASDisplayNode *
ASDisplayNodeFindFirstSupernodeOfClass
(ASDisplayNode *start,
Class
c)
{
return
ASDisplayNodeFindFirstSupernode
(start, ^(ASDisplayNode *n) {
return
[n
isKindOfClass:
c];
});
}
static
void
_ASCollectDisplayNodes
(
NSMutableArray
*array,
CALayer
*layer)
{
ASDisplayNode *node =
ASLayerToDisplayNode
(layer);
if
(
nil
!= node) {
[array
addObject:
node];
}
for
(
CALayer
*sublayer in layer.
sublayers
)
_ASCollectDisplayNodes
(array, sublayer);
}
extern
NSArray
<ASDisplayNode *> *
ASCollectDisplayNodes
(ASDisplayNode *node)
{
NSMutableArray
*list = [
NSMutableArray
array
];
for
(
CALayer
*sublayer in node.
layer
.
sublayers
) {
_ASCollectDisplayNodes
(list, sublayer);
}
return
list;
}
#
pragma mark
- Find all subnodes
static
void
_ASDisplayNodeFindAllSubnodes
(
NSMutableArray
*array, ASDisplayNode *node,
BOOL
(^block)(ASDisplayNode *node))
{
if
(!node)
return
;
for
(ASDisplayNode *subnode in node.
subnodes
) {
if
(
block
(subnode)) {
[array
addObject:
subnode];
}
_ASDisplayNodeFindAllSubnodes
(array, subnode, block);
}
}
extern
NSArray
<ASDisplayNode *> *
ASDisplayNodeFindAllSubnodes
(ASDisplayNode *start,
BOOL
(^block)(ASDisplayNode *node))
{
NSMutableArray
*list = [
NSMutableArray
array
];
_ASDisplayNodeFindAllSubnodes
(list, start, block);
return
list;
}
extern
NSArray
<__kindof ASDisplayNode *> *
ASDisplayNodeFindAllSubnodesOfClass
(ASDisplayNode *start,
Class
c)
{
return
ASDisplayNodeFindAllSubnodes
(start, ^(ASDisplayNode *n) {
return
[n
isKindOfClass:
c];
});
}
#
pragma mark
- Find first subnode
static
ASDisplayNode *
_ASDisplayNodeFindFirstNode
(ASDisplayNode *startNode,
BOOL
includeStartNode,
BOOL
(^block)(ASDisplayNode *node))
{
for
(ASDisplayNode *subnode in startNode.
subnodes
) {
ASDisplayNode *foundNode =
_ASDisplayNodeFindFirstNode
(subnode,
YES
, block);
if
(foundNode) {
return
foundNode;
}
}
if
(includeStartNode &&
block
(startNode))
return
startNode;
return
nil
;
}
extern
__kindof ASDisplayNode *
ASDisplayNodeFindFirstNode
(ASDisplayNode *startNode,
BOOL
(^block)(ASDisplayNode *node))
{
return
_ASDisplayNodeFindFirstNode
(startNode,
YES
, block);
}
extern
__kindof ASDisplayNode *
ASDisplayNodeFindFirstSubnode
(ASDisplayNode *startNode,
BOOL
(^block)(ASDisplayNode *node))
{
return
_ASDisplayNodeFindFirstNode
(startNode,
NO
, block);
}
extern
__kindof ASDisplayNode *
ASDisplayNodeFindFirstSubnodeOfClass
(ASDisplayNode *start,
Class
c)
{
return
ASDisplayNodeFindFirstSubnode
(start, ^(ASDisplayNode *n) {
return
[n
isKindOfClass:
c];
});
}
static
inline
BOOL
_ASDisplayNodeIsAncestorOfDisplayNode
(ASDisplayNode *possibleAncestor, ASDisplayNode *possibleDescendant)
{
ASDisplayNode *supernode = possibleDescendant;
while
(supernode) {
if
(supernode == possibleAncestor) {
return
YES
;
}
supernode = supernode.
supernode
;
}
return
NO
;
}
extern
ASDisplayNode *
ASDisplayNodeFindClosestCommonAncestor
(ASDisplayNode *node1, ASDisplayNode *node2)
{
ASDisplayNode *possibleAncestor = node1;
while
(possibleAncestor) {
if
(
_ASDisplayNodeIsAncestorOfDisplayNode
(possibleAncestor, node2)) {
break
;
}
possibleAncestor = possibleAncestor.
supernode
;
}
ASDisplayNodeCAssertNotNil
(possibleAncestor,
@"
Could not find a common ancestor between node1:
%@
and node2:
%@
"
, node1, node2);
return
possibleAncestor;
}
extern
ASDisplayNode *
ASDisplayNodeUltimateParentOfNode
(ASDisplayNode *node)
{
//
node <- supernode on each loop
//
previous <- node on each loop where node is not nil
//
previous is the final non-nil value of supernode, i.e. the root node
ASDisplayNode *previousNode = node;
while
((node = [node
supernode
])) {
previousNode = node;
}
return
previousNode;
}
#
pragma mark
- Placeholders
UIColor *
ASDisplayNodeDefaultPlaceholderColor
()
{
static
UIColor *defaultPlaceholderColor;
static
dispatch_once_t
onceToken;
dispatch_once
(&onceToken, ^{
defaultPlaceholderColor = [UIColor
colorWithWhite:
0.95
alpha:
1.0
];
});
return
defaultPlaceholderColor;
}
UIColor *
ASDisplayNodeDefaultTintColor
()
{
static
UIColor *defaultTintColor;
static
dispatch_once_t
onceToken;
dispatch_once
(&onceToken, ^{
defaultTintColor = [UIColor
colorWithRed:
0.0
green:
0.478
blue:
1.0
alpha:
1.0
];
});
return
defaultTintColor;
}
#
pragma mark
- Hierarchy Notifications
void
ASDisplayNodeDisableHierarchyNotifications
(ASDisplayNode *node)
{
[node
__incrementVisibilityNotificationsDisabled
];
}
void
ASDisplayNodeEnableHierarchyNotifications
(ASDisplayNode *node)
{
[node
__decrementVisibilityNotificationsDisabled
];
}
Back
|
FazBrowse Home
|
New Git URL