标签:
#import <MapKit/MapKit.h>
#import "MyPointAnnotation.h" //导入自定义的大头针
@interface MyAnnotationView : MKPinAnnotationView {
MyPointAnnotation *myPointAnnotation;
}
@end
#import "MyAnnotationView.h"
@implementation MyAnnotationView
- (instancetype)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier]) {
//保存参数-传过来的大头针
myPointAnnotation = annotation;
//创建图片
UIImage *image = [UIImage imageNamed:@"pink"];
//这种方式只适合静态图片 ,不适合动态和拖拽动态功能,它们有冲突
//这种方式继承MKAnnotationView 否则没有效果
// NSData *imageData = UIImagePNGRepresentation(image);
// //处理imageData 比例5
// image = [UIImage imageWithData:imageData scale:5];
// //修改当前视图的大小
// self.frame = CGRectMake(0, 0, 40, 40);
// //设置图片
// self.image = image;
// //设置填充模式 按比例填满
// self.contentMode = UIViewContentModeScaleToFill;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(-15, -5, 45, 45)];
imageView.image = image;
[self addSubview:imageView];
//允许气泡弹出
self.canShowCallout = YES;
//拖拽
self.draggable = YES;
//下坠动画
self.animatesDrop = YES;
UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeInfoDark];
[leftButton addTarget:self action:@selector(leftButton) forControlEvents:UIControlEventTouchUpInside];
//设置左边访问view
self.leftCalloutAccessoryView = leftButton;
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeContactAdd];
[rightButton addTarget:self action:@selector(rightButton) forControlEvents:UIControlEventTouchUpInside];
//设置右边访问view
self.rightCalloutAccessoryView = rightButton;
}
return self;
}
- (void)leftButton {
NSLog(@"leftButton:%@",myPointAnnotation.title);
}
- (void)rightButton {
NSLog(@"rightButton:%@",myPointAnnotation.information);
}
标签:
原文地址:http://www.cnblogs.com/mzy123mzy/p/5464099.html