Skip to main content

I have subclassed the MKAnnotatonViewMKAnnotatonView and overrode the initWithAnnotation:resueIdentifierinitWithAnnotation:resueIdentifier and drawRectdrawRect methods to add another image behind the "front" image.

It seems that that is what Apple's MKAnnotationViewMKAnnotationView class reference is suggesting us to do.

Only that it is tricky. If you are subclassing the MKAnnotationViewMKAnnotationView, there is still the image property existing. They have done something about the properly so that it is linked with drawing. Perhaps, they have overridden the drawRect to draw the image AFTER the initialization is done.

Also, if you don't set the image property, the frame of your custom annotationViewannotationView is set to size 0, and the drawRectdrawRect method does not get called.

After I made the answer, I realize that you actually wanted to draw behind MKPinAnnotationViewMKPinAnnotationView. My answer is not that, although it shows where the drawing perhaps should be done. Obviously MKPinAnnottionsMKPinAnnottions has its own drawing method to present a Pin and its shadow.

I have subclassed the MKAnnotatonView and overrode the initWithAnnotation:resueIdentifier and drawRect methods to add another image behind the "front" image.

It seems that that is what Apple's MKAnnotationView class reference is suggesting us to do.

Only that it is tricky. If you are subclassing the MKAnnotationView, there is still the image property existing. They have done something about the properly so that it is linked with drawing. Perhaps, they have overridden the drawRect to draw the image AFTER the initialization is done.

Also, if you don't set the image property, the frame of your custom annotationView is set to size 0, and the drawRect method does not get called.

After I made the answer, I realize that you actually wanted to draw behind MKPinAnnotationView. My answer is not that, although it shows where the drawing perhaps should be done. Obviously MKPinAnnottions has its own drawing method to present a Pin and its shadow.

I have subclassed the MKAnnotatonView and overrode the initWithAnnotation:resueIdentifier and drawRect methods to add another image behind the "front" image.

It seems that that is what Apple's MKAnnotationView class reference is suggesting us to do.

Only that it is tricky. If you are subclassing the MKAnnotationView, there is still the image property existing. They have done something about the properly so that it is linked with drawing. Perhaps, they have overridden the drawRect to draw the image AFTER the initialization is done.

Also, if you don't set the image property, the frame of your custom annotationView is set to size 0, and the drawRect method does not get called.

After I made the answer, I realize that you actually wanted to draw behind MKPinAnnotationView. My answer is not that, although it shows where the drawing perhaps should be done. Obviously MKPinAnnottions has its own drawing method to present a Pin and its shadow.

added 625 characters in body
Source Link
Yoichi
  • 12.3k
  • 2
  • 21
  • 11

I have subclassed the MKAnnotatonView and overrode the initWithAnnotation:resueIdentifier and drawRect methods to add another image behind the "front" image.

It seems that that is what Apple's MKAnnotationView class reference is suggesting us to do.

Only that it is tricky. If you are subclassing the MKAnnotationView, there is still the image property existing. They have done something about the properly so that it is linked with drawing. Perhaps, they have overridden the drawRect to draw the image AFTER the initialization is done.

Also, if you don't set the image property, the frame of your custom annotationView is set to size 0, and the drawRect method does not get called.

Finally, Apple says that the images should be totally filled, i.e. uses a transparent colour for the background of the drawings.

So: In your subclass:

- (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);
}

After I made the answer, I realize that you actually wanted to draw behind MKPinAnnotationView. My answer is not that, although it shows where the drawing perhaps should be done. Obviously MKPinAnnottions has its own drawing method to present a Pin and its shadow.

I think that probably you can retrieve the Pin image from the self.image property. As to the shadow, I am not certain... It may be using OPEN GL drawing method to add the shadow, or simply combining a shadow image.

Finally, the image comes with animation. I am guessing that that is where the animation is run. At this stage, though, I have not tested.

I have subclassed the MKAnnotatonView and overrode the initWithAnnotation:resueIdentifier and drawRect methods to add another image behind the "front" image.

It seems that that is what Apple's MKAnnotationView class reference is suggesting us to do.

Only that it is tricky. If you are subclassing the MKAnnotationView, there is still the image property existing. They have done something about the properly so that it is linked with drawing. Perhaps, they have overridden the drawRect to draw the image AFTER the initialization is done.

Also, if you don't set the image property, the frame of your custom annotationView is set to size 0, and the drawRect method does not get called.

Finally, Apple says that the images should be totally filled, i.e. uses a transparent colour for the background of the drawings.

So: In your subclass:

- (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);
}

I have subclassed the MKAnnotatonView and overrode the initWithAnnotation:resueIdentifier and drawRect methods to add another image behind the "front" image.

It seems that that is what Apple's MKAnnotationView class reference is suggesting us to do.

Only that it is tricky. If you are subclassing the MKAnnotationView, there is still the image property existing. They have done something about the properly so that it is linked with drawing. Perhaps, they have overridden the drawRect to draw the image AFTER the initialization is done.

Also, if you don't set the image property, the frame of your custom annotationView is set to size 0, and the drawRect method does not get called.

Finally, Apple says that the images should be totally filled, i.e. uses a transparent colour for the background of the drawings.

So: In your subclass:

- (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);
}

After I made the answer, I realize that you actually wanted to draw behind MKPinAnnotationView. My answer is not that, although it shows where the drawing perhaps should be done. Obviously MKPinAnnottions has its own drawing method to present a Pin and its shadow.

I think that probably you can retrieve the Pin image from the self.image property. As to the shadow, I am not certain... It may be using OPEN GL drawing method to add the shadow, or simply combining a shadow image.

Finally, the image comes with animation. I am guessing that that is where the animation is run. At this stage, though, I have not tested.

added 1442 characters in body; added 132 characters in body
Source Link
Yoichi
  • 12.3k
  • 2
  • 21
  • 11

How did it go with MathieuK's suggestion?

Although it may draw the way you hoped, I presume thathave subclassed the MyCustomAnnotationView does not respond to events, such as touch events,MKAnnotatonView and overrode the initWithAnnotation:resueIdentifier and drawRect methods to bring outadd another image behind the callout, or"front" image.

It seems that that is what Apple's MKAnnotationView class reference is suggesting us to be "selected" etcdo.

Also, importantlyOnly that it is tricky. If you are subclassing the MKAnnotationView, I thinkthere is still the image property existing. They have done something about the properly so that it needs to respondeis linked with drawing. Perhaps, they have overridden the drawRect to reuseIdentifier butdraw the MyCustomeAnnotationView won'timage AFTER the initialization is done.

SoAlso, I guess that itif you don't set the image property, the frame of your custom annotationView is betterset to subclass MKAnnotationViewsize 0, and draw images with implementing the drawRect method does not get called. In it

Finally, you can drawApple says that the "front" image overimages should be totally filled, i.e. uses a transparent colour for the image that you want to use behind itbackground of the drawings.

So: In your subclass:

- (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);
}

How did it go with MathieuK's suggestion?

Although it may draw the way you hoped, I presume that the MyCustomAnnotationView does not respond to events, such as touch events, to bring out the callout, or to be "selected" etc.

Also, importantly, I think that it needs to responde to reuseIdentifier but the MyCustomeAnnotationView won't.

So, I guess that it is better to subclass MKAnnotationView and draw images with implementing the drawRect method. In it, you can draw the "front" image over the image that you want to use behind it.

I have subclassed the MKAnnotatonView and overrode the initWithAnnotation:resueIdentifier and drawRect methods to add another image behind the "front" image.

It seems that that is what Apple's MKAnnotationView class reference is suggesting us to do.

Only that it is tricky. If you are subclassing the MKAnnotationView, there is still the image property existing. They have done something about the properly so that it is linked with drawing. Perhaps, they have overridden the drawRect to draw the image AFTER the initialization is done.

Also, if you don't set the image property, the frame of your custom annotationView is set to size 0, and the drawRect method does not get called.

Finally, Apple says that the images should be totally filled, i.e. uses a transparent colour for the background of the drawings.

So: In your subclass:

- (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);
}
Source Link
Yoichi
  • 12.3k
  • 2
  • 21
  • 11
Loading