Iphone 在MKPinAnnotationView后面添加图像
我正在尝试在MKPinAnnotationView后面添加图像。似乎在这里做这件事应该很容易:Iphone 在MKPinAnnotationView后面添加图像,iphone,mkmapview,mkpinannotationview,Iphone,Mkmapview,Mkpinannotationview,我正在尝试在MKPinAnnotationView后面添加图像。似乎在这里做这件事应该很容易: - (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views { for (MKAnnotationView *aView in views) [[aView superview] addSubview:imageView]; } 但我在这样做时遇到的问题是,pin的子视图将在其顶部而不是后面进行渲染 我
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
for (MKAnnotationView *aView in views)
[[aView superview] addSubview:imageView];
}
但我在这样做时遇到的问题是,pin的子视图将在其顶部而不是后面进行渲染
我也尝试过:
for (MKAnnotationView *aView in views)
[[aView superview] insertSubview:imageView atIndex:1];
这样做的问题是,当它位于图钉后面时,一旦地图被重新定位,图像就会浮离屏幕
有什么建议吗?谢谢创建一个新的复合注释视图,首先添加图像,然后添加实际的MKAnnotationView:
@implementation MyCustomAnnotationView
- (void) viewDidLoad
{
// First add the image to our subviews
UIImageView *view = [[UIImageView alloc] initWithImage: myImageProperty];
[self.view addSubview: view];
[view release];
// Then add the MKAnnotationView on top of it
MKAnnotationView *annotation = [[MKAnnotationView alloc] init];
[self.view addSubview: annotation];
[annotation release];
}
@end
感谢您的输入,以下是我在没有子类化的情况下完成的基本操作:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id )annotation {
MKAnnotationView *annView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
UIImage *image = [UIImage imageNamed:@"image.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[annView addSubview:imageView];
[imageView release];
MKPinAnnotationView *pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
[annView addSubview:pinView];
[pinView release];
return annView;
}
我只需要一个pin,所以我将
reuseIdentifier
设置为nil
我对MKAnnotatonView
进行了子类化,并重写了initWithAnnotation:resueIdentifier
和drawRect
方法,以便在“前”图像后面添加另一个图像
这似乎就是苹果的MKAnnotationView
类引用建议我们做的
只是这很棘手。如果您正在子类化MKAnnotationView
,则仍然存在图像属性。他们已经做了一些适当的工作,使之与绘图相联系。也许,在初始化完成后,它们已经覆盖了drawRect来绘制图像
此外,如果未设置图像属性,则自定义的注释视图
的框架将设置为大小0,并且不会调用drawRect
方法
最后,苹果公司表示,图像应该完全填充,即使用透明的颜色作为图纸的背景
所以:在你的子类中:
- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)identifier
{
self = [super initWithAnnotation:annotation reuseIdentifier:identifier];
if (self)
{
UIButton *rightButton = [UIButton buttonWithType: UIButtonTypeDetailDisclosure];
[rightButton addTarget:self
action:@selector(myShowAnnotationAddress:)
forControlEvents:UIControlEventTouchUpInside];
self.rightCalloutAccessoryView = rightButton;
self.canShowCallout = YES;
self.multipleTouchEnabled = NO;
self.frame = CGRectMake(0, 0, 65, 100);
self.backgroundColor = [UIColor clearColor];
return self;
}
return nil;
}
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGPoint drawPoint = CGPointMake(0.0f, 0.0f);
UIImage *shadowImage = [UIImage imageNamed:@"shadow_image.png"];
[shadowImage drawAtPoint:drawPoint];
UIImage *frontImage = [UIImage imageNamed:@"front_image.png"];
[frontImage drawAtPoint:drawPoint];
CGContextRestoreGState(context);
}
-(id)initWithAnnotation:(id)注释重用标识符:(NSString*)标识符
{
self=[super initWithAnnotation:annotation reuseIdentifier:identifier];
如果(自我)
{
UIButton*rightButton=[UIButton Button类型:UIButtonTypedTailButton];
[rightButton添加目标:自我
操作:@选择器(myShowAnnotationAddress:)
forControlEvents:UIControlEventTouchUpInside];
self.rightCalloutAccessoryView=rightButton;
self.canShowCallout=是;
self.multipletouchedabled=否;
self.frame=CGRectMake(0,0,65,100);
self.backgroundColor=[UIColor clearColor];
回归自我;
}
返回零;
}
-(void)drawRect:(CGRect)rect
{
CGContextRef context=UIGraphicsGetCurrentContext();
CGContextSaveGState(上下文);
CGPoint drawPoint=CGPointMake(0.0f,0.0f);
UIImage*shadowImage=[UIImage ImageName:@“shadow_image.png”];
[阴影图像绘制点:绘制点];
UIImage*frontImage=[UIImage ImageName:@“front_image.png”];
[frontImage drawAtPoint:drawPoint];
CGContextRestoreGState(上下文);
}
在我给出答案后,我意识到您实际上想在MKPinAnnotationView
后面画。我的答案不是这样,尽管它表明了可能应该在哪里画画。显然,MKPinAnnottions
有自己的绘图方法来显示管脚及其阴影
我认为您可能可以从self.image属性中检索Pin图像。至于影子,我不确定。。。可以使用OpenGL绘图方法添加阴影,也可以简单地组合阴影图像
最后,图像附带动画。我猜这就是动画运行的地方。但是,在这个阶段,我还没有测试。如何隐藏pin进行此操作?如果没有MKPinAnnotationView,MKAnnotationView将不会响应触摸。如何解决这个问题?