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
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
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.
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 ...
}
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
é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ő.
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)
}
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
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 ...