Skip to content

Commit 912bc9d

Browse files
committed
Fade out placeholders
Added an API to fade out a node's placeholder when it is finished rendering. fixes facebookarchive#156
1 parent 4886d3d commit 912bc9d

4 files changed

Lines changed: 28 additions & 5 deletions

File tree

AsyncDisplayKit/ASDisplayNode.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,13 @@
338338
*/
339339
@property (nonatomic, assign) BOOL placeholderEnabled;
340340

341+
/**
342+
* @abstract Toggle to fade-out the placeholder when a node's contents are finished displaying.
343+
*
344+
* @discussion Defaults to NO.
345+
*/
346+
@property (nonatomic, assign) BOOL placeholderFadesOut;
347+
341348

342349
/** @name Hit Testing */
343350

AsyncDisplayKit/ASDisplayNode.mm

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ - (void)_initializeInstance
116116
_flags.implementsDrawRect = ([[self class] respondsToSelector:@selector(drawRect:withParameters:isCancelled:isRasterizing:)] ? 1 : 0);
117117
_flags.implementsImageDisplay = ([[self class] respondsToSelector:@selector(displayWithParameters:isCancelled:)] ? 1 : 0);
118118
_flags.implementsDrawParameters = ([self respondsToSelector:@selector(drawParametersForAsyncLayer:)] ? 1 : 0);
119+
120+
_fadeAnimationDuration = 0.1;
119121
}
120122

121123
- (id)init
@@ -1133,7 +1135,19 @@ - (void)_pendingNodeDidDisplay:(ASDisplayNode *)node
11331135
// only trampoline if there is a placeholder and nodes are done displaying
11341136
if ([self _pendingDisplayNodesHaveFinished] && _placeholderLayer.superlayer) {
11351137
dispatch_async(dispatch_get_main_queue(), ^{
1136-
[self _tearDownPlaceholderLayer];
1138+
void (^cleanupBlock)() = ^{
1139+
[self _tearDownPlaceholderLayer];
1140+
};
1141+
1142+
if (self.placeholderFadesOut) {
1143+
[CATransaction begin];
1144+
[CATransaction setCompletionBlock:cleanupBlock];
1145+
[CATransaction setAnimationDuration:_fadeAnimationDuration];
1146+
_placeholderLayer.opacity = 0.0;
1147+
[CATransaction commit];
1148+
} else {
1149+
cleanupBlock();
1150+
}
11371151
});
11381152
}
11391153
}
@@ -1257,10 +1271,6 @@ - (void)displayDidFinish
12571271
[self _pendingNodeDidDisplay:self];
12581272

12591273
[_supernode subnodeDisplayDidFinish:self];
1260-
1261-
if (_placeholderLayer && [self _pendingDisplayNodesHaveFinished]) {
1262-
[self _tearDownPlaceholderLayer];
1263-
}
12641274
}
12651275

12661276
- (void)subnodeDisplayWillStart:(ASDisplayNode *)subnode

AsyncDisplayKit/Private/ASDisplayNodeInternal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ void ASDisplayNodePerformBlockOnMainThread(void (^block)());
6161

6262
_ASPendingState *_pendingViewState;
6363

64+
NSTimeInterval _fadeAnimationDuration;
65+
6466
struct {
6567
// public properties
6668
unsigned synchronous:1;

examples/Placeholders/Sample/SlowpokeImageNode.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ - (instancetype)init
2828
{
2929
if (self = [super init]) {
3030
self.placeholderEnabled = YES;
31+
self.fadeOutPlaceholder = YES;
3132
}
3233
return self;
3334
}
@@ -46,8 +47,11 @@ - (UIImage *)placeholderImage
4647
{
4748
CGSize size = self.calculatedSize;
4849
UIGraphicsBeginImageContext(size);
50+
[[UIColor whiteColor] setFill];
4951
[[UIColor colorWithWhite:0.9 alpha:1] setStroke];
5052

53+
UIRectFill((CGRect){CGPointZero, size});
54+
5155
UIBezierPath *path = [UIBezierPath bezierPath];
5256
[path moveToPoint:CGPointZero];
5357
[path addLineToPoint:(CGPoint){size.width, size.height}];

0 commit comments

Comments
 (0)