MKPinAnnotationViewの後ろに画像を追加
-
19-09-2019 - |
質問
私はMKPinAnnotationViewの背後にイメージを追加しようとしています。ちょうどここでこれを行うにはかなり簡単なはずのように思えるます:
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
for (MKAnnotationView *aView in views)
[[aView superview] addSubview:imageView];
}
しかし、私はそれを行うには持っています問題は、ピンに子サブビューはありませんその背後に、その上にレンダリングされるということです。
私も試してみました。
for (MKAnnotationView *aView in views)
[[aView superview] insertSubview:imageView atIndex:1];
これに伴う問題は、それがピンの後ろにありながら、できるだけ早くマップが再配置されるように、画像が画面の外に浮かぶ、ということである。
任意の提案ですか?おかげ
解決
の入力のためのおかげで、ここで私はサブクラス化せずにやってしまったものを基本的にです。
- (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;
}
私は1つのピンのみを必要なので、私はreuseIdentifier
するnil
を設定します。
他のヒント
私はMKAnnotatonView
をサブクラス化し、「前」の画像の後ろに別の画像を追加するinitWithAnnotation:resueIdentifier
とdrawRect
メソッドをオーバーライドしている。
それがAppleのMKAnnotationView
クラス参照を行うために私達を示唆しているものと思われます。
それはトリッキーであることだけ。あなたがMKAnnotationView
をサブクラス化している場合は、まだ既存のイメージプロパティがあります。それは、図面にリンクされているように、彼らは適切について何かを行っています。おそらく、彼らは初期化が行われた後の画像を描画するためのdrawRectをオーバーライドしています。
また、カスタムannotationView
のフレームはサイズ0に設定され、かつdrawRect
メソッドが呼び出されません。
最後に、Appleはすなわち、図面の背景のために透明色を使用して、イメージが完全に満たされるべきであると述べています。
だから:あなたのサブクラスでます:
- (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);
}
私は答えをした後、、私はあなたが実際にMKPinAnnotationView
後ろに描きたかったことを実現します。それは、描画がおそらく行われるべき場所を示しているが私の答えは、それではありません。明らかにMKPinAnnottions
はピンとその影を提示する独自の描画方法があります。
私はおそらくあなたがself.imageプロパティからピン画像を取得することができると思います。影のように、私は影を追加するには、OPEN GL描画方法を使用して、または単に影の画像を合成することができる...確かではないよ。
最後に、画像がアニメーションが付属しています。私はアニメーションが実行された場合それがあることを推測しています。この段階では、しかし、私がテストしていません。
最初のイメージと、実際のMKAnnotationViewを追加する新しい複合annotationviewを作成します。
@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