سؤال

أحاول إضافة صورة خلف 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;
}

أنا فقط بحاجة دبوس واحد، لذلك أنا وضعت reuseIdentifier ل nil.

نصائح أخرى

لقد صهرت MKAnnotatonView وتجاوز initWithAnnotation:resueIdentifier و drawRect طرق لإضافة صورة أخرى خلف الصورة "الأمامية".

يبدو أن هذا هو ما أبل MKAnnotationView مرجع الطبقة يقترحنا أن نفعل.

فقط أن الأمر صعب. إذا كنت فئة فرعية MKAnnotationView, ، لا تزال هناك خاصية الصورة الموجودة. لقد قاموا بشيء حيال ذلك بحيث يرتبط بالرسم. ربما، لقد تجاوزوا الدرجة لرسم الصورة بعد الانتهاء من التهيئة.

أيضا، إذا لم تقم بتعيين خاصية الصورة، فإن إطار العرف الخاص بك 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 لديه طريقة الرسم الخاصة بها لتقديم دبوس وظلها.

أعتقد أنه ربما يمكنك استرداد صورة PIN من خاصية Self.Image. بالنسبة إلى الظل، أنا لست متأكدا ... قد يستخدم طريقة رسم GL مفتوحة لإضافة الظل، أو ببساطة الجمع بين صورة الظل.

أخيرا، تأتي الصورة مع الرسوم المتحركة. أنا أظن أن هذا هو المكان الذي يتم فيه تشغيل الرسوم المتحركة. في هذه المرحلة، لم أختبر.

قم بإنشاء AnnotationView مركب جديد يضيف صورتك أولا ثم 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  
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top