Swift,访问子类中的父属性

时间:2014-11-27 04:03:40

标签: ios class inheritance swift mkoverlay

我的问题是我不知道如何访问子类中的父属性

class imageOverlayRenderer: MKOverlayRenderer {
var bar = self.overlay
}

MKOverlayRenderer应具有属性覆盖

我真正想要的是编写MKOverlayRender的自定义子类来渲染图像,Objective C中的代码是:

#import <MapKit/MapKit.h>

@interface PVParkMapOverlayView : MKOverlayRenderer

- (instancetype)initWithOverlay:(id<MKOverlay>)overlay overlayImage:(UIImage *)overlayImage;

@end

#import "PVParkMapOverlayView.h"

@interface PVParkMapOverlayView ()

@property (nonatomic, strong) UIImage *overlayImage;

@end

@implementation PVParkMapOverlayView

- (instancetype)initWithOverlay:(id<MKOverlay>)overlay overlayImage:(UIImage *)overlayImage {
    self = [super initWithOverlay:overlay];
    if (self) {
        _overlayImage = overlayImage;
    }

    return self;
}

- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context {
    CGImageRef imageReference = self.overlayImage.CGImage;

    MKMapRect theMapRect = self.overlay.boundingMapRect;
    CGRect theRect = [self rectForMapRect:theMapRect];

    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextTranslateCTM(context, 0.0, -theRect.size.height);
    CGContextDrawImage(context, theRect, imageReference);
}

@end

1 个答案:

答案 0 :(得分:0)

您无法在正常的属性声明中引用self - 您必须声明属性lazy

class imageOverlayRenderer: MKOverlayRenderer {
    lazy var bar: MKOverlay! = self.overlay
}