iOS自定义注释:注释引脚下方的视图 [英] iOS Custom Annotation: A view below the annotation pin

查看:112
本文介绍了iOS自定义注释:注释引脚下方的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我需要做以下事情:


  1. 自定义注释视图,其中嵌入了图像视图。

  2. 下面的视图,其中包含标签。

有关更多说明,请参阅图片:





在上图中,我需要在白色空间放置一个图像视图,你可以在循环形式的图像中看到,接下来我还需要添加一个视图,其中包含一个标签,我可以在其上设置任何文本,如我,朋友等...



所以,为此,我搜索了有关堆栈溢出的问题,但没有得到我的答案。我不希望它在呼出时,我只是想在渲染地图时将其简单地作为注释。我已经尝试为此制作一个自定义类,但不知道如何处理这个问题。



任何帮助都将受到高度赞赏

解决方案

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

  @import MapKit; 

@interface CustomAnnotationView:MKAnnotationView

@end

@interface CustomAnnotationView()
@property(nonatomic)CGSize textSize;
@property(nonatomic)CGSize textBubbleSize;
@property(非原子,弱)UILabel *标签;
@property(非原子)CGFloat lineWidth;
@property(非原子)CGFloat pinRadius;
@property(非原子)CGFloat pinHeight;

@property(非原子,强)UIBezierPath * pinPath;
@property(非原子,强)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;
}
返回自我;
}

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

- (void)adjustLabelWidth:(id< MKAnnotation>)注释{
NSString * 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];

//标签周围的泡沫

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

//中心白点

self.pinPath = [UIBezierPath bezierPathWithArcCenter:中心半径:radius / 3.0 startAngle:0 endAngle:顺时针M_PI * 2.0:真正];
self.pinPath.lineWidth = self.lineWidth;
[[UIColor whiteColor] setFill];
[self.pinPath fill];
}

- (UIView *)hitTest:(CGPoint)指向withEvent :(可空的UIEvent *)事件{
if([self.pinPath containsPoint:point] || [ self.textBubblePath containsPoint:point])
return self;

返回零;
}

@end

产生的结果如下: / p>



显然,您可以根据自己的心情自定义内容,但它说明了基本的想法:写一个 MKAnnotationView 子类,它重写 initWithAnnotation:reuseIdentifier:并实现你自己的 drawRect


I need to replace the default annotation view with my custom annotation view.

I need the do following things:

  1. Custom Annotation view with an image view embedded in it.
  2. A view below it which contains a label in it.

For more clarification see the image:

In the above image I need to place an image view in the white space which you can see in the image in circular form, next I also need to add a view which contains a label on which I can set any text like me, friends, etc...

So, for this I searched number of questions on stack overflow but didn't got my answer. I don't want it on call out, I just want it simply as annotation when map is rendered. I have tried to make a custom class for this but not getting any idea how to deal with this.

Any help will be highly appreciated

解决方案

You could just create your own annotation view:

@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

That yields something like:

Clearly, you can customize this to your heart's content, but it illustrates the basic idea: Write a MKAnnotationView subclass that overrides initWithAnnotation:reuseIdentifier: and implement your own drawRect.

这篇关于iOS自定义注释:注释引脚下方的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆