日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ios点击大头针气泡不弹出_iOS高德地图之自定义大头针and泡泡view

發布時間:2023/12/3 编程问答 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ios点击大头针气泡不弹出_iOS高德地图之自定义大头针and泡泡view 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

啥都不說先看效果圖demo

IMG_0270.PNG

先來說說如何自定義大頭針以及點擊大頭針時彈出的泡泡view

一 : 自定義大頭針

新建CustomAnnotationView 繼承自MAAnnotationView

添加屬性

重寫- (id)initWithAnnotation:(id)annotation reuseIdentifier:(NSString *)reuseIdentifier

重寫- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event 解決泡泡view超出父控件事件響應問題

重寫- (void)setSelected:(BOOL)selected animated:(BOOL)animated

二 : 自定義泡泡View

新建自定義氣泡類 CustomCalloutView,繼承 UIView。

在 CustomCalloutView.h 中定義數據屬性,包含:圖片、商戶名和商戶地址。(隨便你怎么搞,在這里我就搞了一個xib)

Snip20170620_1.png

在上面新建的CustomAnnotationView.h中定義自定義氣泡屬性

#import "CustomCalloutView.h"

@interface CustomAnnotationView : MAAnnotationView

@property (nonatomic, readonly) CustomCalloutView *calloutView;

@end

重寫選中方法- (void)setSelected:(BOOL)selected animated:(BOOL)animated。選中時新建并添加calloutView,傳入數據;非選中時刪除calloutView。

- (void)setSelected:(BOOL)selected animated:(BOOL)animated

{

if (self.selected == selected)

{

return;

}

if (selected)

{

if (self.calloutView == nil)

{

/* Construct custom callout. */

self.calloutView = [CustomCalloutView calloutView];

self.calloutView.frame = 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);

}

[self addSubview:self.calloutView];

}

else

{

[self.calloutView removeFromSuperview];

}

[super setSelected:selected animated:animated];

}

修改ViewController.m,在MAMapViewDelegate的回調方法mapView:viewForAnnotation中的修改annotationView的類型

#pragma mark - MAMapViewDelegate

- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id)annotation

{

if ([annotation isKindOfClass:[MAPointAnnotation class]])

{

static NSString *customReuseIndetifier = @"customReuseIndetifier";

CustomAnnotationView *annotationView = (CustomAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:customReuseIndetifier];

if (annotationView == nil)

{

annotationView = [[CustomAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:customReuseIndetifier];

// must set to NO, so we can show the custom callout view.

annotationView.canShowCallout = NO;

annotationView.draggable = YES;

annotationView.calloutOffset = CGPointMake(0, -5);

}

return annotationView;

}

return nil;

}

用于調整泡泡view顯示不全問題

- (void)mapView:(MAMapView *)mapView didSelectAnnotationView:(MAAnnotationView *)view

{

/* Adjust the map center in order to show the callout view completely. */

if ([view isKindOfClass:[CustomAnnotationView class]]) {

CustomAnnotationView *cusView = (CustomAnnotationView *)view;

CGRect frame = [cusView convertRect:cusView.calloutView.frame toView:self.mapView];

frame = UIEdgeInsetsInsetRect(frame, UIEdgeInsetsMake(kCalloutViewMargin, kCalloutViewMargin, kCalloutViewMargin, kCalloutViewMargin));

if (!CGRectContainsRect(self.mapView.frame, frame))

{

/* Calculate the offset to make the callout view show up. */

CGSize offset = [self offsetToContainRect:frame inRect:self.mapView.frame];

CGPoint theCenter = self.mapView.center;

theCenter = CGPointMake(theCenter.x - offset.width, theCenter.y - offset.height);

CLLocationCoordinate2D coordinate = [self.mapView convertPoint:theCenter toCoordinateFromView:self.mapView];

[self.mapView setCenterCoordinate:coordinate animated:YES];

}

}

}

總結

以上是生活随笔為你收集整理的ios点击大头针气泡不弹出_iOS高德地图之自定义大头针and泡泡view的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。