From 82f7956bf91656f530d8e1568dfbdb41e02e280a Mon Sep 17 00:00:00 2001 From: Scott Goodson Date: Sat, 23 Jan 2016 20:12:45 -0800 Subject: [PATCH] [ASMapNode] Some improvements to layout logic and snapshot triggering. --- AsyncDisplayKit/ASMapNode.h | 3 ++- AsyncDisplayKit/ASMapNode.mm | 30 ++++++++++++++++++++++++++---- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/AsyncDisplayKit/ASMapNode.h b/AsyncDisplayKit/ASMapNode.h index 301859d9bb..8703ac2c1a 100644 --- a/AsyncDisplayKit/ASMapNode.h +++ b/AsyncDisplayKit/ASMapNode.h @@ -36,7 +36,8 @@ NS_ASSUME_NONNULL_BEGIN @property (nonatomic, assign, getter=isLiveMap) BOOL liveMap; /** - @abstract Whether ASMapNode should automatically request a new map snapshot to correspond to the new node size. Defaults to YES. + @abstract Whether ASMapNode should automatically request a new map snapshot to correspond to the new node size. + @default Default value is YES. @discussion If mapSize is set then this will be set to NO, since the size will be the same in all orientations. */ @property (nonatomic, assign) BOOL needsMapReloadOnBoundsChange; diff --git a/AsyncDisplayKit/ASMapNode.mm b/AsyncDisplayKit/ASMapNode.mm index 714df66d57..fcfd821487 100644 --- a/AsyncDisplayKit/ASMapNode.mm +++ b/AsyncDisplayKit/ASMapNode.mm @@ -117,7 +117,10 @@ if (!_options) { _options = [[MKMapSnapshotOptions alloc] init]; _options.region = MKCoordinateRegionForMapRect(MKMapRectWorld); - _options.size = self.calculatedSize; + CGSize calculatedSize = self.calculatedSize; + if (!CGSizeEqualToSize(calculatedSize, CGSizeZero)) { + _options.size = calculatedSize; + } } return _options; } @@ -250,6 +253,24 @@ } #pragma mark - Layout +- (void)setSnapshotSizeIfNeeded:(CGSize)snapshotSize +{ + if (!CGSizeEqualToSize(self.options.size, snapshotSize)) { + _options.size = snapshotSize; + [self resetSnapshotter]; + } +} + +- (CGSize)calculateSizeThatFits:(CGSize)constrainedSize +{ + CGSize size = self.preferredFrameSize; + if (CGSizeEqualToSize(size, CGSizeZero)) { + size = constrainedSize; + } + [self setSnapshotSizeIfNeeded:size]; + return constrainedSize; +} + // Layout isn't usually needed in the box model, but since we are making use of MKMapView this is preferred. - (void)layout { @@ -258,9 +279,10 @@ _mapView.frame = CGRectMake(0.0f, 0.0f, self.calculatedSize.width, self.calculatedSize.height); } else { // If our bounds.size is different from our current snapshot size, then let's request a new image from MKMapSnapshotter. - if (!CGSizeEqualToSize(_options.size, self.bounds.size) && _needsMapReloadOnBoundsChange) { - _options.size = self.bounds.size; - [self resetSnapshotter]; + if (_needsMapReloadOnBoundsChange) { + [self setSnapshotSizeIfNeeded:self.bounds.size]; + // FIXME: Adding a check for FetchData here seems to cause intermittent map load failures, but shouldn't. + // if (ASInterfaceStateIncludesFetchData(self.interfaceState)) { [self takeSnapshot]; } }