Skip to content

Commit

Permalink
Move layout of ASVideoNode to layout specs
Browse files Browse the repository at this point in the history
- This should fix adjusting a custom play button position
- Fix sample project use of ASVideoNode
  • Loading branch information
maicki committed May 5, 2016
1 parent 62a853b commit cac4ed5
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 91 deletions.
111 changes: 72 additions & 39 deletions AsyncDisplayKit/ASVideoNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ @interface ASVideoNode ()

ASImageNode *_placeholderImageNode; // TODO: Make ASVideoNode an ASImageNode subclass; remove this.

ASButtonNode *_playButton;
ASButtonNode *_playButtonNode;
ASDisplayNode *_playerNode;
ASDisplayNode *_spinner;
ASDisplayNode *_spinnerNode;
NSString *_gravity;
}

Expand Down Expand Up @@ -185,24 +185,55 @@ - (void)removePlayerItemObservers:(AVPlayerItem *)playerItem
[notificationCenter removeObserver:self name:AVPlayerItemNewErrorLogEntryNotification object:playerItem];
}

- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize
{
// All subnodes should taking the whole node frame
CGSize maxSize = constrainedSize.max;
if (!CGSizeEqualToSize(self.preferredFrameSize, CGSizeZero)) {
maxSize = self.preferredFrameSize;
}

// Prevent crashes through if infinite width or height
if (isinf(maxSize.width) || isinf(maxSize.height)) {
ASDisplayNodeAssert(NO, @"Infinite width or height in ASVideoNode");
maxSize = CGSizeZero;
}

// Stretch out play button, placeholder image player node to the max size
NSMutableArray *children = [NSMutableArray array];
if (_playButtonNode) {
_playButtonNode.preferredFrameSize = maxSize;
[children addObject:_playButtonNode];
}
if (_placeholderImageNode) {
_placeholderImageNode.preferredFrameSize = maxSize;
[children addObject:_placeholderImageNode];
}
if (_playerNode) {
_playerNode.preferredFrameSize = maxSize;
[children addObject:_playerNode];
}

// Center spinner node
if (_spinnerNode) {
ASCenterLayoutSpec *centerLayoutSpec = [ASCenterLayoutSpec centerLayoutSpecWithCenteringOptions:ASCenterLayoutSpecCenteringXY sizingOptions:ASCenterLayoutSpecSizingOptionDefault child:_spinnerNode];
centerLayoutSpec.sizeRange = ASRelativeSizeRangeMakeWithExactCGSize(maxSize);
[children addObject:_spinnerNode];
}

return [ASStaticLayoutSpec staticLayoutSpecWithChildren:children];
}

- (void)layout
{
[super layout];

CGRect bounds = self.bounds;

ASDN::MutexLocker l(_videoLock);

_placeholderImageNode.frame = bounds;
_playerNode.frame = bounds;
_playButton.frame = bounds;

CGFloat horizontalDiff = (bounds.size.width - _playButton.bounds.size.width)/2;
CGFloat verticalDiff = (bounds.size.height - _playButton.bounds.size.height)/2;
_playButton.hitTestSlop = UIEdgeInsetsMake(-verticalDiff, -horizontalDiff, -verticalDiff, -horizontalDiff);

_spinner.bounds = CGRectMake(0, 0, 44, 44);
_spinner.position = CGPointMake(bounds.size.width/2, bounds.size.height/2);
CGFloat horizontalDiff = (CGRectGetWidth(bounds) - CGRectGetWidth(_playButtonNode.bounds))/2;
CGFloat verticalDiff = (CGRectGetHeight(bounds) - CGRectGetHeight(_playButtonNode.bounds))/2;
_playButtonNode.hitTestSlop = UIEdgeInsetsMake(-verticalDiff, -horizontalDiff, -verticalDiff, -horizontalDiff);
}

- (void)generatePlaceholderImage
Expand Down Expand Up @@ -370,6 +401,7 @@ - (void)visibilityDidChange:(BOOL)isVisible


#pragma mark - Video Properties

- (void)setPlayerState:(ASVideoNodePlayerState)playerState
{
ASDN::MutexLocker l(_videoLock);
Expand All @@ -385,28 +417,26 @@ - (void)setPlayerState:(ASVideoNodePlayerState)playerState
}

_playerState = playerState;

}

- (void)setPlayButton:(ASButtonNode *)playButton
{
ASDN::MutexLocker l(_videoLock);

[_playButton removeTarget:self action:@selector(tapped) forControlEvents:ASControlNodeEventTouchUpInside];
[_playButton removeFromSupernode];
[_playButtonNode removeTarget:self action:@selector(tapped) forControlEvents:ASControlNodeEventTouchUpInside];
[_playButtonNode removeFromSupernode];

_playButtonNode = playButton;
[_playButtonNode addTarget:self action:@selector(tapped) forControlEvents:ASControlNodeEventTouchUpInside];

_playButton = playButton;

[self addSubnode:playButton];

[_playButton addTarget:self action:@selector(tapped) forControlEvents:ASControlNodeEventTouchUpInside];
[self setNeedsLayout];
}

- (ASButtonNode *)playButton
{
ASDN::MutexLocker l(_videoLock);

return _playButton;
return _playButtonNode;
}

- (void)setAsset:(AVAsset *)asset
Expand Down Expand Up @@ -471,14 +501,12 @@ - (void)setGravity:(NSString *)gravity
- (NSString *)gravity
{
ASDN::MutexLocker l(_videoLock);

return _gravity;
}

- (BOOL)muted
{
ASDN::MutexLocker l(_videoLock);

return _muted;
}

Expand Down Expand Up @@ -507,19 +535,21 @@ - (void)play
if (_playerNode == nil) {
_playerNode = [self constructPlayerNode];

if (_playButton.supernode == self) {
[self insertSubnode:_playerNode belowSubnode:_playButton];
if (_playButtonNode.supernode == self) {
[self insertSubnode:_playerNode belowSubnode:_playButtonNode];
} else {
[self addSubnode:_playerNode];
}

[self setNeedsLayout];
}


[_player play];
_shouldBePlaying = YES;

[UIView animateWithDuration:0.15 animations:^{
_playButton.alpha = 0.0;
_playButtonNode.alpha = 0.0;
}];
if (![self ready]) {
[self showSpinner];
Expand All @@ -538,28 +568,29 @@ - (void)showSpinner
{
ASDN::MutexLocker l(_videoLock);

if (!_spinner) {
_spinner = [[ASDisplayNode alloc] initWithViewBlock:^UIView *{
if (!_spinnerNode) {
_spinnerNode = [[ASDisplayNode alloc] initWithViewBlock:^UIView *{
UIActivityIndicatorView *spinnnerView = [[UIActivityIndicatorView alloc] init];
spinnnerView.color = [UIColor whiteColor];

return spinnnerView;
}];

[self addSubnode:_spinner];
_spinnerNode.preferredFrameSize = CGSizeMake(44.0, 44.0);

[self addSubnode:_spinnerNode];
[self setNeedsLayout];
}
[(UIActivityIndicatorView *)_spinner.view startAnimating];
[(UIActivityIndicatorView *)_spinnerNode.view startAnimating];
}

- (void)removeSpinner
{
ASDN::MutexLocker l(_videoLock);

if (!_spinner) {
if (!_spinnerNode) {
return;
}
[_spinner removeFromSupernode];
_spinner = nil;
[_spinnerNode removeFromSupernode];
_spinnerNode = nil;
}

- (void)pause
Expand All @@ -573,7 +604,7 @@ - (void)pause
[self removeSpinner];
_shouldBePlaying = NO;
[UIView animateWithDuration:0.15 animations:^{
_playButton.alpha = 1.0;
_playButtonNode.alpha = 1.0;
}];
}

Expand Down Expand Up @@ -634,7 +665,7 @@ - (void)errorWhilePlaying:(NSNotification *)notification
- (ASDisplayNode *)spinner
{
ASDN::MutexLocker l(_videoLock);
return _spinner;
return _spinnerNode;
}

- (ASImageNode *)placeholderImageNode
Expand Down Expand Up @@ -670,6 +701,8 @@ - (void)setPlayerNode:(ASDisplayNode *)playerNode
{
ASDN::MutexLocker l(_videoLock);
_playerNode = playerNode;

[self setNeedsLayout];
}

- (void)setPlayer:(AVPlayer *)player
Expand Down Expand Up @@ -698,7 +731,7 @@ - (void)dealloc
{
[_player removeTimeObserver:_timeObserver];
_timeObserver = nil;
[_playButton removeTarget:self action:@selector(tapped) forControlEvents:ASControlNodeEventTouchUpInside];
[_playButtonNode removeTarget:self action:@selector(tapped) forControlEvents:ASControlNodeEventTouchUpInside];
[self removePlayerItemObservers:_currentPlayerItem];
}

Expand Down
Loading

0 comments on commit cac4ed5

Please sign in to comment.