iOS自定义标注:在标注图钉下面的视图

3

我需要用自定义注释视图替换默认的注释视图。

我需要做以下事情:

  1. 自定义带有图像视图的注释视图。
  2. 在其下方添加一个包含标签的视图。

为了更清楚,请参见下面的图片:

样例Pin

在上面的图片中,我需要在圆形的白色空间中放置一个图像视图。接下来,我还需要添加一个包含标签的视图,在该标签上可以设置任何文本,如me,friends等...

因此,我在stackoverflow上搜索了许多问题,但没有找到答案。我不想要call out,我只想在地图呈现时简单地作为注释。我尝试过为此创建一个自定义类,但是不知道如何处理它。

非常感谢您的任何帮助。


使用以下网址https://dev59.com/f2w05IYBdhLWcg3w11W0,在图像上添加文本,然后将该输出图像用作注释标记。 - aBilal17
这个问题与自定义注解有什么关联? - iYoung
你想要创建带有文本的图像,对吗? - aBilal17
我在谈论的是引脚而不是标注视图。 - iYoung
让我们在聊天中继续这个讨论 - aBilal17
显示剩余2条评论
1个回答

7

你可以创建自己的注释视图:

@import MapKit;

@interface CustomAnnotationView : MKAnnotationView

@end

@interface CustomAnnotationView ()
@property (nonatomic) CGSize textSize;
@property (nonatomic) CGSize textBubbleSize;
@property (nonatomic, weak) UILabel *label;
@property (nonatomic) CGFloat lineWidth;
@property (nonatomic) CGFloat pinRadius;
@property (nonatomic) CGFloat pinHeight;

@property (nonatomic, strong) UIBezierPath *pinPath;
@property (nonatomic, strong) UIBezierPath *textBubblePath;
@end

@implementation CustomAnnotationView

- (instancetype)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
    if (self) {
        self.lineWidth = 1.0;
        self.pinHeight = 40;
        self.pinRadius = 15;

        UILabel *label = [[UILabel alloc] init];
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont preferredFontForTextStyle:UIFontTextStyleCallout];
        label.textColor = [UIColor whiteColor];
        [self addSubview:label];
        self.label = label;

        [self adjustLabelWidth:annotation];

        self.opaque = false;
    }
    return self;
}

- (void)setAnnotation:(id<MKAnnotation>)annotation {
    [super setAnnotation:annotation];
    if (annotation) [self adjustLabelWidth:annotation];
}

- (void)adjustLabelWidth:(id<MKAnnotation>)annotation {
    NSString *title = [annotation title];
    NSDictionary *attributes = @{NSFontAttributeName : self.label.font};
    self.textSize = [title sizeWithAttributes:attributes];
    CGFloat delta = self.textSize.height * (1.0 - sinf(M_PI_4)) * 0.55;
    self.textBubbleSize = CGSizeMake(self.textSize.width + delta * 2, self.textSize.height + delta * 2);
    self.label.frame = CGRectMake(0, self.pinHeight, self.textBubbleSize.width, self.textBubbleSize.height);
    self.label.text = title;
    self.frame = CGRectMake(0, 0, self.textBubbleSize.width, self.pinHeight + self.textBubbleSize.height);
    self.centerOffset = CGPointMake(0, self.frame.size.height / 2.0 - self.pinHeight);
}

- (void)drawRect:(CGRect)rect {
    CGFloat radius = self.pinRadius - self.lineWidth / 2.0;
    CGPoint startPoint = CGPointMake(self.textBubbleSize.width / 2.0, self.pinHeight);
    CGPoint center = CGPointMake(self.textBubbleSize.width / 2, self.pinRadius);
    CGPoint nextPoint;

    // pin

    self.pinPath = [UIBezierPath bezierPath];
    [self.pinPath moveToPoint:startPoint];
    nextPoint = CGPointMake(self.textBubbleSize.width / 2 - radius, self.pinRadius);
    [self.pinPath addCurveToPoint:nextPoint
                    controlPoint1:CGPointMake(startPoint.x, startPoint.y - (startPoint.y - nextPoint.y) / 2.0)
                    controlPoint2:CGPointMake(nextPoint.x, nextPoint.y + (startPoint.y - nextPoint.y) / 2.0)];

    [self.pinPath addArcWithCenter:center radius:radius startAngle:M_PI endAngle:0 clockwise:TRUE];
    nextPoint = startPoint;
    startPoint = self.pinPath.currentPoint;
    [self.pinPath addCurveToPoint:nextPoint
                    controlPoint1:CGPointMake(startPoint.x, startPoint.y - (startPoint.y - nextPoint.y) / 2.0)
                    controlPoint2:CGPointMake(nextPoint.x, nextPoint.y + (startPoint.y - nextPoint.y) / 2.0)];
    [[UIColor blackColor] setStroke];
    [[UIColor colorWithRed:0.0 green:0.5 blue:1.0 alpha:0.8] setFill];
    self.pinPath.lineWidth = self.lineWidth;
    [self.pinPath fill];
    [self.pinPath stroke];
    [self.pinPath closePath];

    // bubble around label

    if ([self.annotation.title length] > 0) {
        self.textBubblePath = [UIBezierPath bezierPath];
        CGRect bubbleRect = CGRectInset(CGRectMake(0, self.pinHeight, self.textBubbleSize.width, self.textBubbleSize.height), self.lineWidth / 2, self.lineWidth / 2);
        self.textBubblePath = [UIBezierPath bezierPathWithRoundedRect:bubbleRect
                                                         cornerRadius:bubbleRect.size.height / 2];
        self.textBubblePath.lineWidth = self.lineWidth;
        [self.textBubblePath fill];
        [self.textBubblePath stroke];
    } else {
        self.textBubblePath = nil;
    }

    // center white dot

    self.pinPath = [UIBezierPath bezierPathWithArcCenter:center radius:radius / 3.0 startAngle:0 endAngle:M_PI * 2.0 clockwise:TRUE];
    self.pinPath.lineWidth = self.lineWidth;
    [[UIColor whiteColor] setFill];
    [self.pinPath fill];
}

- (UIView *)hitTest:(CGPoint)point withEvent:(nullable UIEvent *)event {
    if ([self.pinPath containsPoint:point] || [self.textBubblePath containsPoint:point])
        return self;

    return nil;
}

@end

这将产生类似于以下内容:

enter image description here

显然,您可以根据自己的需要对其进行自定义,但它说明了基本思想:编写一个MKAnnotationView子类,覆盖initWithAnnotation:reuseIdentifier:并实现自己的drawRect

我们能否在viewforannotation委托方法中返回一个自定义视图?其中自定义视图将包含两个图像视图。 - LC 웃
是的,您也可以创建自定义图像并将其用于注释视图的图像。实际上,如果注释视图对于不同的注释是相同的,则更可取。但在我的示例中,每个注释都是不同的,因此自定义drawRect是有意义的。顺便说一句,即使您使用自定义图像而不是drawRect,您也可以在MKAnnotationView子类(代替自定义drawRect)或viewForAnnotation中执行此操作。在我看来,这种复杂的渲染不适合放在viewForAnnotation中,甚至不适合放在地图视图委托中。 - Rob
@RajatDeepSingh 加入我的聊天室 http://chat.stackoverflow.com/rooms/79037/ios-custom-annotation-a-view-below-the-annotation-pin - Rob
1
对于在iOS 11下绘制自定义注释遇到问题的人,请确保为注释视图设置大小,就像在这个优秀的示例中一样。 - FKDev

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接