yandexmobile / yandexmapkit-ios

Yandex Map Kit for iOS
Other
173 stars 30 forks source link

Функция calloutViewFor annotation: YMKAnnotation! кэшируется #194

Open Chingiz-888 opened 7 years ago

Chingiz-888 commented 7 years ago

Проблема такая. Я хочу ловить события по кликам на пины - аннотации

Есть функция func mapView(_ mapView: YMKMapView!, calloutViewFor annotation: YMKAnnotation!) -> YMKCalloutView! {

Из которой можно вытянуть annotation

Но! Эта функция вызывается для данной аннотации только ОДИН РАЗ, далее эта функция не вызывается

Как еще по-другому ловить событие тапа на аннотацию

gagarina4 commented 7 years ago

Решение для Obj (нашел в тикетах)

- (YMKAnnotationView *)mapView:(YMKMapView *)mapView viewForAnnotation:(id<YMKAnnotation>)annotation  {
static NSString * identifier = @"pointAnnotation";
YMKPinAnnotationView * view = (YMKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (view == nil) {
view = [[YMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
view.canShowCallout = YES;

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
tap.numberOfTapsRequired = 1;
[view addGestureRecognizer:tap];
}

view.annotation = annotation;

return view;
}

- (void)tap:(UITapGestureRecognizer *)tap
{
YMKPinAnnotationView *view = (YMKPinAnnotationView *)tap.view;
// делаем что-то
// открываем callout, эта строчка не нужна если не хочешь открывать callout
self.mapView.selectedAnnotation = view.annotation;
}

Решение для swift

func mapView(_ mapView: YMKMapView!, viewFor annotation: YMKAnnotation!) -> YMKAnnotationView! {
    let identifier = "pointAnnotation";
    let view = YMKPinAnnotationView.init(annotation: annotation, reuseIdentifier: identifier)
    view?.canShowCallout = true
    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(tap(aPan:)))
    tapGesture.numberOfTapsRequired = 1
    view?.addGestureRecognizer(tapGesture)
    view?.annotation = annotation
    return view
}

func tap(aPan:UITapGestureRecognizer) {
    print ("Annotation2 Selected")
    let view = aPan.view as! YMKPinAnnotationView
    self.mapView.selectedAnnotation = view.annotation
}