MKAnnotationView with custom view and image view

拜拜、爱过 提交于 2019-12-03 16:34:47

The default frame width and height for MKAnnotationView is 0,0.
This is most likely preventing it from responding to touches.

Normally, if you set its image property, the frame is automatically set for you.

Since you're not setting the image and adding subviews instead, try manually setting frame to be at least as big as its largest subview.

For example:

imgView.frame = r;
circleView.frame = r;
annotationView.frame = r;  // <-- add this line

I created a sub class of annotation view and achieved it. The code is below:

@interface TStickerAnnotationView : MKAnnotationView

@property(nonatomic) float stickerColor;

@end

@interface TStickerAnnotationView () {
    UIImageView *_imageView;
    TCircleView *_circleView;
}


@end

@implementation TStickerAnnotationView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // make sure the x and y of the CGRect are half it's
        // width and height, so the callout shows when user clicks
        // in the middle of the image
        CGRect  viewRect = CGRectMake(-30, -30, 60, 60);

        TCircleView* circleView = [[TCircleView alloc] initWithFrame:viewRect];
        _circleView = circleView;
        [self addSubview:circleView];

        UIImageView* imageView = [[UIImageView alloc] initWithFrame:viewRect];

        // keeps the image dimensions correct
        // so if you have a rectangle image, it will show up as a rectangle,
        // instead of being resized into a square
        imageView.contentMode = UIViewContentModeScaleAspectFit;

        _imageView = imageView;

        [self addSubview:imageView];



    }
    return self;
}

- (void)setImage:(UIImage *)image
{
    // when an image is set for the annotation view,
    // it actually adds the image to the image view
    _imageView.image = image;
}

- (void)stickerColor:(float)color {
    _circleView.green = color;
    [_circleView setNeedsDisplay];
}

Try looking at this solution- I found it relevant http://blog.jaanussiim.com/2014/01/28/floating-annotations.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!