[ios] Fix annotation initializers for subclasses of MGLAnnotationView (#9104)
Use a common init function in both of the provided initializers so that subclasses of `MGLAnnotationView` written in Swift don't need to override `init(annotation, reuseIdentifier)`
This commit is contained in:
@@ -172,6 +172,7 @@
|
||||
408AA8571DAEDA1700022900 /* NSDictionary+MGLAdditions.h in Headers */ = {isa = PBXBuildFile; fileRef = 408AA8551DAEDA0800022900 /* NSDictionary+MGLAdditions.h */; };
|
||||
408AA8581DAEDA1E00022900 /* NSDictionary+MGLAdditions.mm in Sources */ = {isa = PBXBuildFile; fileRef = 408AA8561DAEDA0800022900 /* NSDictionary+MGLAdditions.mm */; };
|
||||
408AA8591DAEDA1E00022900 /* NSDictionary+MGLAdditions.mm in Sources */ = {isa = PBXBuildFile; fileRef = 408AA8561DAEDA0800022900 /* NSDictionary+MGLAdditions.mm */; };
|
||||
409D0A0D1ED614CE00C95D0C /* MGLAnnotationViewIntegrationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 409D0A0C1ED614CE00C95D0C /* MGLAnnotationViewIntegrationTests.swift */; };
|
||||
409F43FD1E9E781C0048729D /* MGLMapViewDelegateIntegrationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 409F43FC1E9E781C0048729D /* MGLMapViewDelegateIntegrationTests.swift */; };
|
||||
40CF6DBB1DAC3C6600A4D18B /* MGLShape_Private.h in Headers */ = {isa = PBXBuildFile; fileRef = 40CF6DBA1DAC3C1800A4D18B /* MGLShape_Private.h */; };
|
||||
40CFA6511D7875BB008103BD /* MGLShapeSourceTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 40CFA6501D787579008103BD /* MGLShapeSourceTests.mm */; };
|
||||
@@ -639,6 +640,7 @@
|
||||
4085AF081D933DEA00F11B22 /* MGLTileSetTests.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = MGLTileSetTests.mm; path = ../../darwin/test/MGLTileSetTests.mm; sourceTree = "<group>"; };
|
||||
408AA8551DAEDA0800022900 /* NSDictionary+MGLAdditions.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "NSDictionary+MGLAdditions.h"; sourceTree = "<group>"; };
|
||||
408AA8561DAEDA0800022900 /* NSDictionary+MGLAdditions.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = "NSDictionary+MGLAdditions.mm"; sourceTree = "<group>"; };
|
||||
409D0A0C1ED614CE00C95D0C /* MGLAnnotationViewIntegrationTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MGLAnnotationViewIntegrationTests.swift; sourceTree = "<group>"; };
|
||||
409F43FC1E9E781C0048729D /* MGLMapViewDelegateIntegrationTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MGLMapViewDelegateIntegrationTests.swift; sourceTree = "<group>"; };
|
||||
40CF6DBA1DAC3C1800A4D18B /* MGLShape_Private.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MGLShape_Private.h; sourceTree = "<group>"; };
|
||||
40CFA6501D787579008103BD /* MGLShapeSourceTests.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; name = MGLShapeSourceTests.mm; path = ../../darwin/test/MGLShapeSourceTests.mm; sourceTree = "<group>"; };
|
||||
@@ -1119,6 +1121,7 @@
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
409F43FC1E9E781C0048729D /* MGLMapViewDelegateIntegrationTests.swift */,
|
||||
409D0A0C1ED614CE00C95D0C /* MGLAnnotationViewIntegrationTests.swift */,
|
||||
);
|
||||
name = "Swift Integration";
|
||||
sourceTree = "<group>";
|
||||
@@ -2148,6 +2151,7 @@
|
||||
1F95931D1E6DE2E900D5B294 /* MGLNSDateAdditionsTests.mm in Sources */,
|
||||
DD58A4C61D822BD000E1F038 /* MGLExpressionTests.mm in Sources */,
|
||||
3575798B1D502B0C000B822E /* MGLBackgroundStyleLayerTests.mm in Sources */,
|
||||
409D0A0D1ED614CE00C95D0C /* MGLAnnotationViewIntegrationTests.swift in Sources */,
|
||||
DA2E88621CC0382C00F24E7B /* MGLOfflinePackTests.m in Sources */,
|
||||
55E2AD131E5B125400E8C587 /* MGLOfflineStorageTests.mm in Sources */,
|
||||
920A3E5D1E6F995200C16EFC /* MGLSourceQueryTests.m in Sources */,
|
||||
|
||||
@@ -24,22 +24,29 @@
|
||||
}
|
||||
|
||||
- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier {
|
||||
return [self initWithAnnotation:nil reuseIdentifier:reuseIdentifier];
|
||||
self = [super initWithFrame:CGRectZero];
|
||||
if (self) {
|
||||
[self commonInitWithAnnotation:nil reuseIdentifier:reuseIdentifier];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (instancetype)initWithAnnotation:(nullable id<MGLAnnotation>)annotation reuseIdentifier:(nullable NSString *)reuseIdentifier {
|
||||
self = [super initWithFrame:CGRectZero];
|
||||
if (self)
|
||||
{
|
||||
_lastAppliedScaleTransform = CATransform3DIdentity;
|
||||
_annotation = annotation;
|
||||
_reuseIdentifier = [reuseIdentifier copy];
|
||||
_scalesWithViewingDistance = YES;
|
||||
_enabled = YES;
|
||||
if (self) {
|
||||
[self commonInitWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)commonInitWithAnnotation:(nullable id<MGLAnnotation>)annotation reuseIdentifier:(nullable NSString *)reuseIdentifier {
|
||||
_lastAppliedScaleTransform = CATransform3DIdentity;
|
||||
_annotation = annotation;
|
||||
_reuseIdentifier = [reuseIdentifier copy];
|
||||
_scalesWithViewingDistance = YES;
|
||||
_enabled = YES;
|
||||
}
|
||||
|
||||
- (instancetype)initWithCoder:(NSCoder *)decoder {
|
||||
if (self = [super initWithCoder:decoder]) {
|
||||
_reuseIdentifier = [decoder decodeObjectOfClass:[NSString class] forKey:@"reuseIdentifier"];
|
||||
|
||||
23
platform/ios/test/MGLAnnotationViewIntegrationTests.swift
Normal file
23
platform/ios/test/MGLAnnotationViewIntegrationTests.swift
Normal file
@@ -0,0 +1,23 @@
|
||||
import XCTest
|
||||
import Mapbox
|
||||
|
||||
class CustomAnnotationView: MGLAnnotationView {
|
||||
|
||||
override init(reuseIdentifier: String?) {
|
||||
super.init(reuseIdentifier: reuseIdentifier)
|
||||
}
|
||||
|
||||
required init?(coder aDecoder: NSCoder) {
|
||||
super.init(coder: aDecoder)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class MGLAnnotationViewIntegrationTests: XCTestCase {
|
||||
|
||||
func testCreatingCustomAnnotationView() {
|
||||
let customAnnotationView = CustomAnnotationView(reuseIdentifier: "resuse-id")
|
||||
XCTAssertNotNil(customAnnotationView)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,11 +3,24 @@
|
||||
|
||||
static NSString * const MGLTestAnnotationReuseIdentifer = @"MGLTestAnnotationReuseIdentifer";
|
||||
|
||||
@interface MGLCustomAnnotationView : MGLAnnotationView
|
||||
|
||||
@end
|
||||
|
||||
@implementation MGLCustomAnnotationView
|
||||
|
||||
- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier {
|
||||
return [super initWithReuseIdentifier:@"reuse-id"];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@interface MGLAnnotationView (Test)
|
||||
|
||||
@property (nonatomic) MGLMapView *mapView;
|
||||
@property (nonatomic, readwrite) MGLAnnotationViewDragState dragState;
|
||||
|
||||
- (void)setDragState:(MGLAnnotationViewDragState)dragState;
|
||||
|
||||
@end
|
||||
|
||||
@interface MGLMapView (Test)
|
||||
@@ -79,6 +92,12 @@ static NSString * const MGLTestAnnotationReuseIdentifer = @"MGLTestAnnotationReu
|
||||
XCTAssertNil(_annotationView.annotation, @"annotation property should be nil");
|
||||
}
|
||||
|
||||
- (void)testCustomAnnotationView
|
||||
{
|
||||
MGLCustomAnnotationView *customAnnotationView = [[MGLCustomAnnotationView alloc] initWithReuseIdentifier:@"reuse-id"];
|
||||
XCTAssertNotNil(customAnnotationView);
|
||||
}
|
||||
|
||||
- (MGLAnnotationView *)mapView:(MGLMapView *)mapView viewForAnnotation:(id<MGLAnnotation>)annotation
|
||||
{
|
||||
MGLAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:MGLTestAnnotationReuseIdentifer];
|
||||
|
||||
Reference in New Issue
Block a user