9

sを使用しMKCircleて特定のユーザーアクションの半径情報を表示するマップビューがあります。

私がやりたいのは、ユーザーがMKCircle地図に触れたときにそれを閉じることができるようにすることです。MKCircleただし、ユーザーが他のピンまたはMKCircleそれ自体に触れた場合は、を閉じないようにしたいと思います。

何か案は?

これが私の現在のコードです。これMKCircleは、マップのいずれかの部分がタッチされたときに閉じます。

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(deactivateAllRadars)];
[tap setCancelsTouchesInView:NO];
[_mapView addGestureRecognizer:tap];
4

2 に答える 2

7

このdeactivateAllRadarsメソッドでは、がタップされたhitTest:withEvent:かどうかを判断するために使用できます。MKAnnotationView

この例は、MapView でタップをキャッチしてデフォルトのジェスチャ認識エンジンに渡すにはどうすればよいですか? に示されています。(これは 2 番目のコード サンプルです)。

これにより、注釈がタップされた場合に円を削除することを回避できます。

注釈がタップされていない場合MKCircleは、タッチの座標を取得してタップされたかどうかを確認し (例については、 MKMapViewでタップ ジェスチャをキャプチャする方法を参照)、タッチから円の中心までの距離が大きいかどうかを確認します。その半径よりも。

関連するジェスチャ認識エンジンからの情報が必要になるため、deactivateAllRadarsを に変更する必要があることに注意してください。deactivateAllRadars:(UITapGestureRecognizer *)tgrまた、 alloc+init を指定するメソッドのセレクターの最後に必ずコロンを追加してくださいtap

例えば:

-(void)deactivateAllRadars:(UITapGestureRecognizer *)tgr
{
    CGPoint p = [tgr locationInView:mapView];

    UIView *v = [mapView hitTest:p withEvent:nil];

    id<MKAnnotation> ann = nil;

    if ([v isKindOfClass:[MKAnnotationView class]])
    {
        //annotation view was tapped, select it...
        ann = ((MKAnnotationView *)v).annotation;
        [mapView selectAnnotation:ann animated:YES];
    }
    else
    {
        //annotation view was not tapped, deselect if some ann is selected...
        if (mapView.selectedAnnotations.count != 0)
        {
            ann = [mapView.selectedAnnotations objectAtIndex:0];
            [mapView deselectAnnotation:ann animated:YES];
        }


        //remove circle overlay if it was not tapped...        
        if (mapView.overlays.count > 0)
        {
            CGPoint touchPoint = [tgr locationInView:mapView];

            CLLocationCoordinate2D touchMapCoordinate 
              = [mapView convertPoint:touchPoint toCoordinateFromView:mapView];

            CLLocation *touchLocation = [[CLLocation alloc] 
              initWithLatitude:touchMapCoordinate.latitude 
              longitude:touchMapCoordinate.longitude];

            CLLocation *circleLocation = [[CLLocation alloc] 
              initWithLatitude:circleCenterLatitude 
              longitude:circleCenterLongitude];

            CLLocationDistance distFromCircleCenter 
              = [touchLocation distanceFromLocation:circleLocation];

            if (distFromCircleCenter > circleRadius)
            {
                //tap was outside the circle, call removeOverlay...
            }
        }
    }
}
于 2012-11-21T20:06:54.430 に答える
3

これは私のSwift 2.1互換バージョンです。

func didTapOnMap(recognizer: UITapGestureRecognizer) {
    let tapLocation = recognizer.locationInView(self)
    if let subview = self.hitTest(tapLocation, withEvent: nil) {
        if subview.isKindOfClass(NSClassFromString("MKNewAnnotationContainerView")!) {
            print("Tapped out")
        }
    }
}

MKNewAnnotationContainerViewはプライベートな内部クラスであるため、次のように直接比較することはできません。

if subview is MKNewAnnotationContainerView {
}
于 2016-03-21T13:57:00.033 に答える