Как добавить uibutton в пользовательское представление аннотаций

Я создал пользовательский вид аннотации. В методе setSelected я реализовал вид, который мне нужен, теперь я хочу добавить кнопку в этот вид. Но кнопка, не обнаруживающая событие щелчка, поглощается представлением карты. что мне делать, чтобы добавить кнопку типа rightcalloutaccessory. Заранее спасибо.


person san    schedule 24.10.2011    source источник
comment
Попробуйте ответить на этот вопрос.   -  person    schedule 24.10.2011


Ответы (1)


Я решил эту проблему, изменив управляющее событие для UIButton как

[button addTarget:self action:@selector(clickEvent) forControlEvents:UIControlEventTouchDown]

Ниже приведена часть моего кода для создания пользовательских аннотаций.

    - (CustomAnnotationView *)mapView:(MKMapView *)aMapView viewForAnnotation:(id <MKAnnotation>)annotation
    {
        if ([annotation isKindOfClass:[CustomAnnotation class]])
        {
             CustomAnnotationView *newAnnotationView = nil;
    // determine the type of annotation, and produce the correct type of annotation view for it.

             newAnnotationView = [[[CustomAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil delegate: targetController] autorelease];
             [newAnnotationView setEnabled:YES];
             [newAnnotationView setCanShowCallout:NO];
             return newAnnotationView;
       }
    }





    @interface CustomAnnotationView : MKAnnotationView 
    {
    }
    @end


    @implementation CustomAnnotationView

    - (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier delegate:(id)targetController
    {
            self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
            /* Set initial width as you want. ex: 50X50*/
            self.frame = CGRectMake(0, 0, Initial_width, Initial_height); 
             self.backgroundColor = [UIColor redColor];
             return self;
    }

    /* When you tap on an annotation this method is fired*/ 
    - (void)setSelected:(BOOL)selected animated:(BOOL)animated
    {
        if(selected){
            /* When you select annotation, instead of showing default callout you can increase the width &height of annotation and add your custom view here*/ 
                self.frame = CGRectMake(0, 0, max_width, max_height);
                UIView *view = [[UIView alloc]initWithFrame:CGRectMake(6, 0, max_width-10, max_height)];
                view.backgroundColor = [UIColor blackColor];

                UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom];       
                    UIImage *img = [UIImage imageNamed:@"image.png"];
                but = CGRectMake(x , y, img.size.width + 20,  img.size.height + 15);
                [but setImage:img forState:UIControlStateNormal];
                [but addTarget:targetController action:@selector(methodName) forControlEvents:UIControlEventTouchDown];
                [view addSubview:but];

                [self addSubview:view];
        }
        else {
                /* When you tap outside of annotation, set the initial frame for the annotation. and remove the custom view from annotation*/
                self.frame = CGRectMake(0, 0, Initial_width, Initial_height);
                [view removeFromSuperview];

            }
    }
person san    schedule 22.03.2012
comment
Как вы остановили всплывающую форму сенсорного события до функции mapView:didSelectAnnotationView:? - person jlindenbaum; 03.12.2012
comment
@san, не могли бы вы помочь мне с этим. этот код не работает для меня. - person Muhammad Umar; 05.03.2013
comment
@MuhammadUmar Подробнее см. в отредактированном коде. Надеюсь, это может быть полезно для вас - person san; 11.03.2013
comment
@san, увеличив self.frame, используемый вами штифт не изменит свой размер? - person Muhammad Umar; 11.03.2013
comment
@MuhammadUmar Загрузите пример кода с github. - person san; 14.03.2013
comment
@MuhammadUmar это полезно для тебя? - person san; 22.03.2013