From 1dfb8fac67d0428d7be7fcecb7620892fd4c2542 Mon Sep 17 00:00:00 2001 From: Aaron Schubert Date: Tue, 8 Dec 2015 10:37:00 +0000 Subject: [PATCH] Changed init method, as well as further clean up to get ASMapNode ready for 2.0 --- AsyncDisplayKit.xcodeproj/project.pbxproj | 2 +- AsyncDisplayKit/ASMapNode.h | 2 +- AsyncDisplayKit/ASMapNode.mm | 97 ++++++++++++++--------- 3 files changed, 60 insertions(+), 41 deletions(-) diff --git a/AsyncDisplayKit.xcodeproj/project.pbxproj b/AsyncDisplayKit.xcodeproj/project.pbxproj index 91e8c8b36b..7ec37737ed 100644 --- a/AsyncDisplayKit.xcodeproj/project.pbxproj +++ b/AsyncDisplayKit.xcodeproj/project.pbxproj @@ -517,7 +517,7 @@ 057D02C51AC0A66700C7AC3C /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 057D02C61AC0A66700C7AC3C /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; 0587F9BB1A7309ED00AFF0BA /* ASEditableTextNode.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASEditableTextNode.h; sourceTree = ""; }; - 0587F9BC1A7309ED00AFF0BA /* ASEditableTextNode.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; lineEnding = 0; path = ASEditableTextNode.mm; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.objcpp; }; + 0587F9BC1A7309ED00AFF0BA /* ASEditableTextNode.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; lineEnding = 0; path = ASEditableTextNode.mm; sourceTree = ""; }; 058D09AC195D04C000B7D73C /* libAsyncDisplayKit.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libAsyncDisplayKit.a; sourceTree = BUILT_PRODUCTS_DIR; }; 058D09AF195D04C000B7D73C /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; }; 058D09B3195D04C000B7D73C /* AsyncDisplayKit-Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "AsyncDisplayKit-Prefix.pch"; sourceTree = ""; }; diff --git a/AsyncDisplayKit/ASMapNode.h b/AsyncDisplayKit/ASMapNode.h index e5fcbd4921..d47a1343eb 100644 --- a/AsyncDisplayKit/ASMapNode.h +++ b/AsyncDisplayKit/ASMapNode.h @@ -11,7 +11,7 @@ @interface ASMapNode : ASImageNode -- (instancetype)initWithCoordinate:(CLLocationCoordinate2D)coordinate NS_DESIGNATED_INITIALIZER; +- (instancetype)initWithRegion:(MKCoordinateRegion)region NS_DESIGNATED_INITIALIZER; /** This is the MKMapView that is the live map part of ASMapNode. This will be nil if .liveMap = NO. Note, MKMapView is *not* thread-safe. diff --git a/AsyncDisplayKit/ASMapNode.mm b/AsyncDisplayKit/ASMapNode.mm index 311000362e..39a24a1907 100644 --- a/AsyncDisplayKit/ASMapNode.mm +++ b/AsyncDisplayKit/ASMapNode.mm @@ -28,7 +28,8 @@ @synthesize needsMapReloadOnBoundsChange = _needsMapReloadOnBoundsChange; @synthesize mapDelegate = _mapDelegate; -- (instancetype)initWithCoordinate:(CLLocationCoordinate2D)coordinate +#pragma mark - Lifecycle +- (instancetype)initWithRegion:(MKCoordinateRegion)region { if (!(self = [super init])) { return nil; @@ -41,30 +42,40 @@ _centerCoordinateOfMap = kCLLocationCoordinate2DInvalid; _options = [[MKMapSnapshotOptions alloc] init]; - _options.region = MKCoordinateRegionMakeWithDistance(coordinate, 1000, 1000);; + _options.region = region; return self; } -- (void)setAnnotations:(NSArray *)annotations +- (void)didLoad { - ASDN::MutexLocker l(_propertyLock); - _annotations = [annotations copy]; - if (annotations.count != _annotations.count) { - // Redraw - [self setNeedsDisplay]; - } + [super didLoad]; + if (self.isLiveMap && !_mapNode) { + self.userInteractionEnabled = YES; + [self addLiveMap]; + } } -- (void)setUpSnapshotter +- (void)fetchData { - if (!_snapshotter) { - ASDisplayNodeAssert(!CGSizeEqualToSize(CGSizeZero, self.calculatedSize), @"self.calculatedSize can not be zero. Make sure that you are setting a preferredFrameSize or wrapping ASMapNode in a ASRatioLayoutSpec or similar."); - _options.size = self.calculatedSize; - _snapshotter = [[MKMapSnapshotter alloc] initWithOptions:_options]; - } + [super fetchData]; + if (_liveMap && !_mapNode) { + [self addLiveMap]; + } + else { + [self setUpSnapshotter]; + [self takeSnapshot]; + } } +- (void)clearFetchedData +{ + [super clearFetchedData]; + [self removeLiveMap]; +} + +#pragma mark - Settings + - (BOOL)isLiveMap { ASDN::MutexLocker l(_propertyLock); @@ -94,23 +105,8 @@ _needsMapReloadOnBoundsChange = needsMapReloadOnBoundsChange; } -- (void)fetchData -{ - [super fetchData]; - if (_liveMap && !_mapNode) { - [self addLiveMap]; - } - else { - [self setUpSnapshotter]; - [self takeSnapshot]; - } -} -- (void)clearFetchedData -{ - [super clearFetchedData]; - [self removeLiveMap]; -} +#pragma mark - Snapshotter - (void)takeSnapshot { @@ -150,23 +146,33 @@ } } +- (void)setUpSnapshotter +{ + if (!_snapshotter) { + ASDisplayNodeAssert(!CGSizeEqualToSize(CGSizeZero, self.calculatedSize), @"self.calculatedSize can not be zero. Make sure that you are setting a preferredFrameSize or wrapping ASMapNode in a ASRatioLayoutSpec or similar."); + _options.size = self.calculatedSize; + _snapshotter = [[MKMapSnapshotter alloc] initWithOptions:_options]; + } +} + - (void)resetSnapshotter { if (!_snapshotter.isLoading) { - _options.size = self.calculatedSize; _snapshotter = [[MKMapSnapshotter alloc] initWithOptions:_options]; } } -#pragma mark - Action +#pragma mark - Actions - (void)addLiveMap { if (self.isNodeLoaded && !_mapNode) { - _mapNode = [[ASDisplayNode alloc]initWithViewBlock:^UIView *{ - _mapView = [[MKMapView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, self.calculatedSize.width, self.calculatedSize.height)]; + __weak ASMapNode *weakSelf = self; + _mapNode = [[ASDisplayNode alloc] initWithViewBlock:^UIView *{ + _mapView = [[MKMapView alloc] initWithFrame:CGRectZero]; _mapView.delegate = _mapDelegate; [_mapView setRegion:_options.region]; [_mapView addAnnotations:_annotations]; + [weakSelf setNeedsLayout]; return _mapView; }]; [self addSubnode:_mapNode]; @@ -185,9 +191,23 @@ _mapView = nil; _mapNode = nil; } - self.image = nil; } +- (void)setAnnotations:(NSArray *)annotations +{ + ASDN::MutexLocker l(_propertyLock); + _annotations = [annotations copy]; + if (annotations.count != _annotations.count) { + // Redraw + [self setNeedsDisplay]; + if (_mapView) { + [_mapView removeAnnotations:_mapView.annotations]; + [_mapView addAnnotations:annotations]; + } + } +} + + #pragma mark - Layout // Layout isn't usually needed in the box model, but since we are making use of MKMapView which is hidden in an ASDisplayNode this is preferred. - (void)layout @@ -198,11 +218,10 @@ } 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)) { - if (_needsMapReloadOnBoundsChange && self.image) { + if (!CGSizeEqualToSize(_options.size, self.bounds.size) && _needsMapReloadOnBoundsChange) { + _options.size = self.bounds.size; [self resetSnapshotter]; [self takeSnapshot]; - } } } }