iPhone: észlelése Tap in MKMapView

szavazat
19

Hogyan érzékeli egyetlen érintéssel egy példánya MKMapView? Muszáj alosztályba MKMapViewmajd felülírja a touchesEndedmódszer?

Kösz,

-Chris

A kérdést 14/08/2009 03:22
a forrás felhasználó
Más nyelveken...                            


8 válasz

szavazat
31

Ha csak keres, hogy értesülj csap gesztusok anélkül bármely más érintés viselkedését a térkép, akkor szeretnénk használni UITapGestureRecognizer. Ez szuper egyszerű, csak fel egy kódot, mint ez.

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

Meg fogja hívni az didTapMap, amikor theMKMapViewmegkapja a csapot gesztus, és az összes csipkedte, és húzással gesztusok továbbra is működni fog, mint korábban.

Válaszolt 31/12/2010 21:30
a forrás felhasználó

Válaszolt 10/11/2009 13:27
a forrás felhasználó

szavazat
2

Tökéletesen működik 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 ...
     }
Válaszolt 03/04/2015 10:46
a forrás felhasználó

szavazat
2

Vagy attól függően, hogy mit akar csinálni, adjunk hozzá egy MKAnnotation(rajzszög, egy kiemelő), így van valami, hogy érintse meg -, majd a térkép felhatalmazott kapni fog egy esemény pl.

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

Válaszolt 18/09/2009 09:31
a forrás felhasználó

szavazat
0

én 2 cent Swit 5.x:

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    if let touch = touches.first {
        let v = touch.view
        let ssv = v?.superview?.superview
        if ssv === self.mapView{
            searchBar.resignFirstResponder()
        }
    }
}

működik. de őszintén tönkreteheti, ha az Apple megváltoztatja réteg látható. Jobb a felismerő.

Válaszolt 16/09/2019 19:19
a forrás felhasználó

szavazat
0

Semmit sem valaha talált megmunkált, de én találtam ki ezt a unperfect megoldás: A viewDidLoad

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

A küldött:

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
    return touch.view!.frame.equalTo(mapView.frame)
}
Válaszolt 20/09/2017 09:34
a forrás felhasználó

szavazat
0

Csak adjunk hozzá néhány kódrészletet illusztráció @ tt-kilew választ. Az én esetemben, azt akarom, hogy pont a felhasználó számára, hogy magát a térképen, de nem akarja megszakítani a drag touch.

@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
Válaszolt 07/04/2017 13:59
a forrás felhasználó

szavazat
0

Vidám ebben az időben lefoglaló érinti a térképen, akkor próbálja rétegződés átlátszatlan kilátás van, és ha ez felveszi érinti ...

Válaszolt 14/08/2009 03:59
a forrás felhasználó

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more