문제

인스턴스에 대한 단일 탭을 어떻게 감지합니까? MKMapView? 서브 클래스를해야합니까? MKMapView 그런 다음 touchesEnded 방법?

감사,

-Chris

도움이 되었습니까?

다른 팁

지도의 다른 터치 동작에 영향을 미치지 않고 탭 제스처에 대한 알림을 받으려면 UITapGestureRecognizer. 매우 간단합니다. 이와 같은 코드를 넣으십시오.

UITapGestureRecognizer* tapRec = [[UITapGestureRecognizer alloc] 
   initWithTarget:self action:@selector(didTapMap:)];
[theMKMapView addGestureRecognizer:tapRec];
[tapRec release];

그것은 그것을 부를 것입니다 didTapMap 언제든지 theMKMapView 탭 제스처와 모든 꼬집음을 받고 드래그하는 제스처는 여전히 이전과 마찬가지로 작동합니다.

iOS 8에서 완벽하게 작동합니다

- (void)viewDidLoad 
    {
        [super viewDidLoad];

        UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:nil];
        doubleTap.numberOfTapsRequired = 2;
        doubleTap.numberOfTouchesRequired = 1;
        [self.mapView addGestureRecognizer:doubleTap];

        UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
        singleTap.numberOfTapsRequired = 1;
        singleTap.numberOfTouchesRequired = 1;
        [singleTap requireGestureRecognizerToFail: doubleTap];
        [self.mapView addGestureRecognizer:singleTap];
     }

   - (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer
     {
            if (gestureRecognizer.state != UIGestureRecognizerStateEnded)
                return;
            //Do your work ...
     }

또는 당신이하려는 일에 따라 MKAnnotation (콜 아웃이있는 푸시 핀)이므로 탭해야 할 것이 있습니다. 그러면 맵 대의원은 이벤트를 받게됩니다.

mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control

이 시간에지도보기를 가로 채지 못하고 거기에 불투명 한보기를 레이어링하고 터치를 픽업하는지 확인할 수 있습니다 ...

@tt-kilew 답변의 그림으로 코드 스 니펫을 추가하십시오. 제 경우에는 사용자를지도에서 자신을 가리키고 싶지만 드래그 터치를 방해하고 싶지 않습니다.

@interface PrettyViewController () <MKMapViewDelegate>

@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@property (assign, nonatomic) BOOL userTouchTheMap;

@end

@implementation PrettyViewController

#pragma mark - UIResponder

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];

    self.userTouchTheMap = [[touches anyObject].view isEqual:self.mapView];
}


#pragma mark - MKMapViewDelegate

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
    //We just positioning to user
    if (!self.userTouchTheMap) {
        CLLocationDistance radius = 5000;
        [self.mapView setRegion:MKCoordinateRegionMakeWithDistance(userLocation.location.coordinate, 2*radius, 2*radius) animated:YES];
    }
}

@end

내가 찾은 것은 아무것도 없었지만, 나는이 불완전한 해결책을 생각해 냈습니다 : in ViewDidload

let singleTapRecognizer = UITapGestureRecognizer(target: self, action: #selector(onMapClicked))
singleTapRecognizer.delegate = self
mapView.addGestureRecognizer(singleTapRecognizer)

대의원에서 :

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
    return touch.view!.frame.equalTo(mapView.frame)
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top