高德地圖API點(diǎn)擊氣泡效果是一個(gè)白色的對(duì)話框,像這樣的. 當(dāng)大頭針在屏幕邊緣的時(shí)候,彈出的對(duì)話框是默認(rèn)可見(jiàn)范圍的,也就是說(shuō)那個(gè)對(duì)話框不是在大頭針的center上方出現(xiàn),所以體驗(yàn)不好,而且只能顯示titile 和 sub兩句話.
自定義這個(gè)類(lèi)必須繼承與MAAnnotationView.
下面上代碼:
這是選中非選中的邏輯代碼實(shí)現(xiàn)
- (void)setSelected:(BOOL)selected
{
[self setSelected:selected animated:NO];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated{
if (self.selected == selected)
{
return;
}
if (selected)
{
if (self.calloutView == nil)
{
/* Construct custom callout. */
self.calloutView = [[CustomCalloutView alloc] initWithFrame:CGRectMake(0, 0, kCalloutWidth, kCalloutHeight)];
self.calloutView.center = CGPointMake(CGRectGetWidth(self.bounds) / 2.f + self.calloutOffset.x,
-CGRectGetHeight(self.calloutView.bounds) / 2.f + self.calloutOffset.y);
UILabel * nameLabel? =[[UILabel alloc]initWithFrame:CGRectMake(3, 0 , kCalloutWidth - 3, 20)];
UILabel * timeLabel? =[[UILabel alloc]initWithFrame:CGRectMake(3, 20 , kCalloutWidth - 3, 20)];
UILabel * waterLabel =[[UILabel alloc]initWithFrame:CGRectMake(3, 40 , kCalloutWidth - 3, 20)];
UILabel * QLabel? ? =[[UILabel alloc]initWithFrame:CGRectMake(3, 60 , kCalloutWidth - 3, 20)];
UILabel * numLabel? =[[UILabel alloc]initWithFrame:CGRectMake(kCalloutWidth - 103,0, 100, 20)];
[self setLabelType:nameLabel AndLabel2:timeLabel AndLabel3:waterLabel AndLabel4:QLabel AndLabel5:numLabel];
}
[self addSubview:self.calloutView];
}else
{
[self.calloutView removeFromSuperview];
}
[super setSelected:selected animated:animated];
}
至于其中的CustomCalloutView是我自己畫(huà)的一個(gè)對(duì)話框類(lèi)型的View,效果如下:
這樣就滿(mǎn)足了View是在大頭針的正上方出現(xiàn)的,而且數(shù)據(jù)也可以自定義顯示出來(lái).
下面介紹這個(gè)類(lèi)型對(duì)話框的View的畫(huà)法,其實(shí)可以讓美工直接給圖的,不過(guò)因?yàn)楫?dāng)時(shí)需求特殊,我就自己畫(huà)了一個(gè).下面上代碼:
#import "CustomCalloutView.h"#import#define kArrorHeight? ? 10
@implementation CustomCalloutView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor clearColor];
}
return self;
}
#pragma mark - draw rect
- (void)drawRect:(CGRect)rect
{
[self drawInContext:UIGraphicsGetCurrentContext()];
self.layer.shadowColor = [[UIColor blackColor] CGColor];
self.layer.shadowOpacity = 1.0;
self.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
}
- (void)drawInContext:(CGContextRef)context
{
CGContextSetLineWidth(context, 2.0);
CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
[self getDrawPath:context];
CGContextFillPath(context);
}
- (void)getDrawPath:(CGContextRef)context
{
CGRect rrect = self.bounds;
CGFloat radius = 6.0;
CGFloat minx = CGRectGetMinX(rrect),
midx = CGRectGetMidX(rrect),
maxx = CGRectGetMaxX(rrect);
CGFloat miny = CGRectGetMinY(rrect),
maxy = CGRectGetMaxY(rrect)-kArrorHeight;
CGContextMoveToPoint(context, midx+kArrorHeight, maxy);
CGContextAddLineToPoint(context,midx, maxy+kArrorHeight);
CGContextAddLineToPoint(context,midx-kArrorHeight, maxy);
CGContextAddArcToPoint(context, minx, maxy, minx, miny, radius);
CGContextAddArcToPoint(context, minx, minx, maxx, miny, radius);
CGContextAddArcToPoint(context, maxx, miny, maxx, maxy, radius);
CGContextAddArcToPoint(context, maxx, maxy, midx + kArrorHeight, maxy, radius);
CGContextClosePath(context);
}
無(wú)非是用了CGContext這個(gè)函數(shù),在View上的畫(huà)布,自己根據(jù)需求點(diǎn),描繪圖形