Changed init method, as well as further clean up to get ASMapNode ready for 2.0

This commit is contained in:
Aaron Schubert
2015-12-08 10:37:00 +00:00
parent c9a0a3d46f
commit 1dfb8fac67
3 changed files with 60 additions and 41 deletions

View File

@@ -517,7 +517,7 @@
057D02C51AC0A66700C7AC3C /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; };
057D02C61AC0A66700C7AC3C /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = "<group>"; };
0587F9BB1A7309ED00AFF0BA /* ASEditableTextNode.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASEditableTextNode.h; sourceTree = "<group>"; };
0587F9BC1A7309ED00AFF0BA /* ASEditableTextNode.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; lineEnding = 0; path = ASEditableTextNode.mm; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objcpp; };
0587F9BC1A7309ED00AFF0BA /* ASEditableTextNode.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; lineEnding = 0; path = ASEditableTextNode.mm; sourceTree = "<group>"; };
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 = "<group>"; };

View File

@@ -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.

View File

@@ -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];
}
}
}
}