Domanda

Non sono sicuro di cosa sto facendo di sbagliato, ma provo a catturare tocchi su un oggetto MKMapView. L'ho suddiviso in sottoclassi creando la seguente classe:

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>

@interface MapViewWithTouches : MKMapView {

}

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *) event;   

@end

E l'implementazione:

#import "MapViewWithTouches.h"
@implementation MapViewWithTouches

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *) event {

    NSLog(@"hello");
    //[super touchesBegan:touches   withEvent:event];

}
@end

Ma sembra che quando uso questa classe, non vedo nulla sulla console:

MapViewWithTouches *mapView = [[MapViewWithTouches alloc] initWithFrame:self.view.frame];
[self.view insertSubview:mapView atIndex:0];

Hai idea di cosa sto facendo di sbagliato?

È stato utile?

Soluzione

Il modo migliore che ho trovato per raggiungere questo obiettivo è con un Gesture Recognizer. Altri modi risultano coinvolgere molta programmazione hacker che duplica in modo imperfetto il codice Apple, soprattutto nel caso del multitouch.

Ecco cosa faccio: implementare un riconoscimento gesti che non può essere prevenuto e che non può impedire altri riconoscimenti gestuali. Aggiungilo alla vista della mappa, quindi usa i tocchi del gesto Riconoscitore: Inizia, tocca, ecc., Per la tua fantasia.

Come rilevare qualsiasi tocco all'interno di un MKMapView (sans tricks)

WildcardGestureRecognizer * tapInterceptor = [[WildcardGestureRecognizer alloc] init];
tapInterceptor.touchesBeganCallback = ^(NSSet * touches, UIEvent * event) {
        self.lockedOnUserLocation = NO;
};
[mapView addGestureRecognizer:tapInterceptor];

WildcardGestureRecognizer.h

//
//  WildcardGestureRecognizer.h
//  Copyright 2010 Floatopian LLC. All rights reserved.
//

#import <Foundation/Foundation.h>

typedef void (^TouchesEventBlock)(NSSet * touches, UIEvent * event);

@interface WildcardGestureRecognizer : UIGestureRecognizer {
    TouchesEventBlock touchesBeganCallback;
}
@property(copy) TouchesEventBlock touchesBeganCallback;


@end

WildcardGestureRecognizer.m

//
//  WildcardGestureRecognizer.m
//  Created by Raymond Daly on 10/31/10.
//  Copyright 2010 Floatopian LLC. All rights reserved.
//

#import "WildcardGestureRecognizer.h"


@implementation WildcardGestureRecognizer
@synthesize touchesBeganCallback;

-(id) init{
    if (self = [super init])
    {
        self.cancelsTouchesInView = NO;
    }
    return self;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (touchesBeganCallback)
        touchesBeganCallback(touches, event);
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
}

- (void)reset
{
}

- (void)ignoreTouch:(UITouch *)touch forEvent:(UIEvent *)event
{
}

- (BOOL)canBePreventedByGestureRecognizer:(UIGestureRecognizer *)preventingGestureRecognizer
{
    return NO;
}

- (BOOL)canPreventGestureRecognizer:(UIGestureRecognizer *)preventedGestureRecognizer
{
    return NO;
}

@end

SWIFT 3

let tapInterceptor = WildCardGestureRecognizer(target: nil, action: nil)
tapInterceptor.touchesBeganCallback = {
    _, _ in
    self.lockedOnUserLocation = false
}
mapView.addGestureRecognizer(tapInterceptor)

WildCardGestureRecognizer.swift

import UIKit.UIGestureRecognizerSubclass

class WildCardGestureRecognizer: UIGestureRecognizer {

    var touchesBeganCallback: ((Set<UITouch>, UIEvent) -> Void)?

    override init(target: Any?, action: Selector?) {
        super.init(target: target, action: action)
        self.cancelsTouchesInView = false
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
        super.touchesBegan(touches, with: event)
        touchesBeganCallback?(touches, event)
    }

    override func canPrevent(_ preventedGestureRecognizer: UIGestureRecognizer) -> Bool {
        return false
    }

    override func canBePrevented(by preventingGestureRecognizer: UIGestureRecognizer) -> Bool {
        return false
    }
}

Altri suggerimenti

Dopo una giornata di pizze, urla, ho finalmente trovato la soluzione! Molto pulito!

Peter, ho usato il tuo trucco sopra e l'ho modificato un po 'per avere finalmente una soluzione che funziona perfettamente con MKMapView e dovrebbe funzionare anche con UIWebView

MKTouchAppDelegate.h

#import <UIKit/UIKit.h>
@class UIViewTouch;
@class MKMapView;

@interface MKTouchAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    UIViewTouch *viewTouch;
    MKMapView *mapView;
}
@property (nonatomic, retain) UIViewTouch *viewTouch;
@property (nonatomic, retain) MKMapView *mapView;
@property (nonatomic, retain) IBOutlet UIWindow *window;

@end

MKTouchAppDelegate.m

#import "MKTouchAppDelegate.h"
#import "UIViewTouch.h"
#import <MapKit/MapKit.h>

@implementation MKTouchAppDelegate

@synthesize window;
@synthesize viewTouch;
@synthesize mapView;


- (void)applicationDidFinishLaunching:(UIApplication *)application {

    //We create a view wich will catch Events as they occured and Log them in the Console
    viewTouch = [[UIViewTouch alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];

    //Next we create the MKMapView object, which will be added as a subview of viewTouch
    mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    [viewTouch addSubview:mapView];

    //And we display everything!
    [window addSubview:viewTouch];
    [window makeKeyAndVisible];


}


- (void)dealloc {
    [window release];
    [super dealloc];
}


@end

UIViewTouch.h

#import <UIKit/UIKit.h>
@class UIView;

@interface UIViewTouch : UIView {
    UIView *viewTouched;
}
@property (nonatomic, retain) UIView * viewTouched;

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event;

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;

@end

UIViewTouch.m

#import "UIViewTouch.h"
#import <MapKit/MapKit.h>

@implementation UIViewTouch
@synthesize viewTouched;

//The basic idea here is to intercept the view which is sent back as the firstresponder in hitTest.
//We keep it preciously in the property viewTouched and we return our view as the firstresponder.
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    NSLog(@"Hit Test");
    viewTouched = [super hitTest:point withEvent:event];
    return self;
}

//Then, when an event is fired, we log this one and then send it back to the viewTouched we kept, and voilà!!! :)
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"Touch Began");
    [viewTouched touchesBegan:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"Touch Moved");
    [viewTouched touchesMoved:touches withEvent:event];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"Touch Ended");
    [viewTouched touchesEnded:touches withEvent:event];
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"Touch Cancelled");
}

@end

Spero che possa aiutare alcuni di voi!

Saluti

UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleGesture:)];   
tgr.numberOfTapsRequired = 2;
tgr.numberOfTouchesRequired = 1;
[mapView addGestureRecognizer:tgr];
[tgr release];


- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer
{
    if (gestureRecognizer.state != UIGestureRecognizerStateEnded)
        return;

    CGPoint touchPoint = [gestureRecognizer locationInView:mapView];
    CLLocationCoordinate2D touchMapCoordinate = [mapView convertPoint:touchPoint toCoordinateFromView:mapView];

    //.............
}

Per un MKMapView la vera soluzione funzionante è il riconoscimento dei gesti!

Io volevo smettere di aggiornare il centro della mappa sulla mia posizione quando trascino la mappa o pizzico per ingrandire.

Quindi, crea e aggiungi il tuo riconoscimento dei gesti a mapView:

- (void)viewDidLoad {

    ...

    // Add gesture recognizer for map hoding
    UILongPressGestureRecognizer *longPressGesture = [[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressAndPinchGesture:)] autorelease];
    longPressGesture.delegate = self;
    longPressGesture.minimumPressDuration = 0;  // In order to detect the map touching directly (Default was 0.5)
    [self.mapView addGestureRecognizer:longPressGesture];

    // Add gesture recognizer for map pinching
    UIPinchGestureRecognizer *pinchGesture = [[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressAndPinchGesture:)] autorelease];
    pinchGesture.delegate = self;
    [self.mapView addGestureRecognizer:pinchGesture];

    // Add gesture recognizer for map dragging
    UIPanGestureRecognizer *panGesture = [[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)] autorelease];
    panGesture.delegate = self;
    panGesture.maximumNumberOfTouches = 1;  // In order to discard dragging when pinching
    [self.mapView addGestureRecognizer:panGesture];
}

Guarda Riferimento classe UIGestureRecognizer per vedere tutti i riconoscitori di gesti disponibili.

Poiché abbiamo definito il delegato su sé stesso, dobbiamo implementare il protocollo UIGestureRecognizerDelegate:

typedef enum {
    MapModeStateFree,                    // Map is free
    MapModeStateGeolocalised,            // Map centred on our location
    MapModeStateGeolocalisedWithHeading  // Map centred on our location and oriented with the compass
} MapModeState;

@interface MapViewController : UIViewController <CLLocationManagerDelegate, UIGestureRecognizerDelegate> {
    MapModeState mapMode;
}

@property (nonatomic, retain) IBOutlet MKMapView *mapView;
...

E sovrascrivi il metodo gestureRecognizer: gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer: per consentire il riconoscimento di più gesti contemporaneamente, se ho capito bene:

// Allow to recognize multiple gestures simultaneously (Implementation of the protocole UIGestureRecognizerDelegate)
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}

Ora scrivi i metodi che verranno chiamati dai nostri riconoscitori di gesti:

// On map holding or pinching pause localise and heading
- (void)handleLongPressAndPinchGesture:(UIGestureRecognizer *)sender {
    // Stop to localise and/or heading
    if (sender.state == UIGestureRecognizerStateBegan && mapMode != MapModeStateFree) {
        [locationManager stopUpdatingLocation];
        if (mapMode == MapModeStateGeolocalisedWithHeading) [locationManager stopUpdatingHeading];
    }
    // Restart to localise and/or heading
    if (sender.state == UIGestureRecognizerStateEnded && mapMode != MapModeStateFree) {
        [locationManager startUpdatingLocation];
        if (mapMode == MapModeStateGeolocalisedWithHeading) [locationManager startUpdatingHeading];
    }
}

// On dragging gesture put map in free mode
- (void)handlePanGesture:(UIGestureRecognizer *)sender {
    if (sender.state == UIGestureRecognizerStateBegan && mapMode != MapModeStateFree) [self setMapInFreeModePushedBy:sender];
}

Nel caso in cui qualcuno stia provando a fare lo stesso come me: volevo creare un'annotazione nel punto in cui l'utente tocca. Per questo ho usato la UITapGestureRecognizer soluzione:

UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapOnMap:)];
[self.mapView addGestureRecognizer:tapGestureRecognizer];
[tapGestureRecognizer setDelegate:self];

- (void)didTapOnMap:(UITapGestureRecognizer *)gestureRecognizer
{
    CGPoint point = [gestureRecognizer locationInView:self.mapView];
    CLLocationCoordinate2D coordinate = [self.mapView convertPoint:point toCoordinateFromView:self.mapView];
    .......
}

Tuttavia, didTapOnMap: è stato chiamato anche quando ho toccato l'annotazione e ne sarebbe stata creata una nuova. La soluzione è implementare UIGestureRecognizerDelegate:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if ([touch.view isKindOfClass:[MKAnnotationView class]])
    {
        return NO;
    }
    return YES;
}

Probabilmente dovrai sovrapporre una vista trasparente per catturare i tocchi proprio come accade spesso con i controlli basati su UIWebView. La Visualizzazione mappa fa già un sacco di cose speciali con un tocco per consentire alla mappa di essere spostata, centrata, ingrandita, ecc ... che i messaggi non vengano fatti confluire nella tua app.

Altre due opzioni (NON TESTATE) che mi vengono in mente:

1) Rinuncia al primo risponditore tramite IB e impostalo su " Proprietario file " per consentire al proprietario del file di rispondere ai tocchi. Dubito che funzionerà perché MKMapView estende NSObject, non UIView e di conseguenza gli eventi touch potrebbero non essere propagati fino a te.

2) Se vuoi intercettare quando cambia lo stato della Mappa (come su uno zoom), implementa semplicemente il protocollo MKMapViewDelegate per ascoltare eventi particolari. Il mio sospetto è che questo è il tuo colpo migliore per intrappolare facilmente alcune interazioni (a meno di implementare la vista trasparente sulla mappa). Non dimenticare di impostare View Controller che ospita MKMapView come delegato della mappa (map.delegate = self).

Buona fortuna.

Non ho sperimentato, ma c'è una buona probabilità che MapKit sia basato su un cluster di classi, quindi la sottoclasse è difficile e inefficace.

Suggerirei di trasformare MapKit in una vista secondaria di una vista personalizzata, che dovrebbe consentire di intercettare gli eventi tocco prima che raggiungano esso.

Quindi dopo mezza giornata di pasticciare con questo ho trovato quanto segue:

  1. Come hanno scoperto tutti gli altri, il pizzicamento non funziona. Ho provato sia la sottoclasse MKMapView sia il metodo sopra descritto (intercettandolo). E il risultato è lo stesso.
  2. Nei video dell'iPhone di Stanford, un ragazzo di Apple afferma che molte delle cose di UIKit lo faranno causa molti errori se " trasferisci " il tocco richiede (ovvero i due metodi sopra descritti) e probabilmente non riuscirai a farlo funzionare.

  3. LA SOLUZIONE : è descritta qui: Intercettazione / dirottamento di eventi touch iPhone per MKMapView . Fondamentalmente & Quot; cattura & Quot; l'evento prima che un soccorritore lo ottenga e lo interpreti lì.

In Swift 3.0

import UIKit
import MapKit

class CoordinatesPickerViewController: UIViewController {

    @IBOutlet var mapView: MKMapView!
    override func viewDidLoad() {
        super.viewDidLoad()

        let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(clickOnMap))
        mapView.addGestureRecognizer(tapGestureRecognizer)
    }

    @objc func clickOnMap(_ sender: UITapGestureRecognizer) {

        if sender.state != UIGestureRecognizerState.ended { return }
        let touchLocation = sender.location(in: mapView)
        let locationCoordinate = mapView.convert(touchLocation, toCoordinateFrom: mapView)
        print("Tapped at lat: \(locationCoordinate.latitude) long: \(locationCoordinate.longitude)")

    }

}

Trasforma MKMapView in una vista secondaria di una vista e implementa personalizzata

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event

nella visualizzazione personalizzata per restituire self anziché la subview.

Grazie per la pizza e le urla - mi hai risparmiato un sacco di tempo.

multipletouchenabled funzionerà sporadicamente.

viewTouch.multipleTouchEnabled = TRUE;

Alla fine, ho cambiato le viste quando avevo bisogno di catturare il tocco (diverso nel tempo rispetto al bisogno di pinchzoom):

    [mapView removeFromSuperview];
    [viewTouch addSubview:mapView];
    [self.view insertSubview:viewTouch atIndex:0];

Ho notato che puoi tenere traccia del numero e della posizione dei tocchi e ottenere la posizione di ciascuno in una vista:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"Touch Moved %d", [[event allTouches] count]);

 NSEnumerator *enumerator = [touches objectEnumerator];
 id value;

 while ((value = [enumerator nextObject])) {
  NSLog(@"touch description %f", [value locationInView:mapView].x);
 }
    [viewTouched touchesMoved:touches withEvent:event];
}

Qualcun altro ha provato a utilizzare questi valori per aggiornare il livello di zoom della mappa? Si tratterebbe di registrare le posizioni di partenza, quindi le posizioni di arrivo, calcolare la differenza relativa e aggiornare la mappa.

Sto giocando con il codice di base fornito da Martin e sembra che funzioni ...

Ecco cosa ho messo insieme, che consente di ingrandire lo zoom nel simulatore (non ho provato su un vero iPhone), ma penso che andrebbe bene:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"Touch Began %d", [touches count]);
 reportTrackingPoints = NO;
 startTrackingPoints = YES;
    [viewTouched touchesBegan:touches withEvent:event];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
 if ([[event allTouches] count] == 2) {
  reportTrackingPoints = YES;
  if (startTrackingPoints == YES) {
   BOOL setA = NO;
   NSEnumerator *enumerator = [[event allTouches] objectEnumerator];
   id value;
   while ((value = [enumerator nextObject])) {
    if (! setA) {
     startPointA = [value locationInView:mapView];
     setA = YES;
    } else {
     startPointB = [value locationInView:mapView];
    }
   }
   startTrackingPoints = NO;
  } else {
   BOOL setA = NO;
   NSEnumerator *enumerator = [[event allTouches] objectEnumerator];
   id value;
   while ((value = [enumerator nextObject])) {
    if (! setA) {
     endPointA = [value locationInView:mapView];
     setA = YES;
    } else {
     endPointB = [value locationInView:mapView];
    }
   }
  }
 }
 //NSLog(@"Touch Moved %d", [[event allTouches] count]);
    [viewTouched touchesMoved:touches withEvent:event];
}

- (void) updateMapFromTrackingPoints {
 float startLenA = (startPointA.x - startPointB.x);
 float startLenB = (startPointA.y - startPointB.y);
 float len1 = sqrt((startLenA * startLenA) + (startLenB * startLenB));
 float endLenA = (endPointA.x - endPointB.x);
 float endLenB = (endPointA.y - endPointB.y);
 float len2 = sqrt((endLenA * endLenA) + (endLenB * endLenB));
 MKCoordinateRegion region = mapView.region;
 region.span.latitudeDelta = region.span.latitudeDelta * len1/len2;
 region.span.longitudeDelta = region.span.longitudeDelta * len1/len2;
 [mapView setRegion:region animated:YES];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
 if (reportTrackingPoints) {
  [self updateMapFromTrackingPoints];
  reportTrackingPoints = NO;
 }


    [viewTouched touchesEnded:touches withEvent:event];
}

L'idea principale è che se l'utente utilizza due dita, segui i valori. Registro i punti iniziale e finale nei punti di inizio A e B. Quindi registro i punti di tracciamento correnti e quando ho finito, al tocco, posso chiamare una routine per calcolare le lunghezze relative della linea tra i punti con cui inizio e la linea tra il punto in cui finisco con l'utilizzo di ipotenuse semplice calc. Il rapporto tra loro è la quantità di zoom: moltiplico la regione per quell'importo.

Spero sia utile a qualcuno.

Ho preso l'idea di un " overlay " visione trasparente, dalla risposta di MystikSpiral, e ha funzionato perfettamente per quello che stavo cercando di ottenere; soluzione rapida e pulita.

In breve, avevo un UITableViewCell personalizzato (progettato in IB) con un MKMapView sul lato sinistro e alcuni UILabel sulla destra. Volevo creare la cella personalizzata in modo da poterla toccare ovunque e questo avrebbe spinto un nuovo controller di visualizzazione. Tuttavia, toccare la mappa non ha passato i tocchi "up" a UITableViewCell fino a quando non ho semplicemente aggiunto una UIView delle stesse dimensioni della vista della mappa proprio sopra di essa (in IB) e ho reso lo sfondo il "colore chiaro" nel codice ( non pensare di poter impostare clearColor in IB ??):

dummyView.backgroundColor = [UIColor clearColor];

Ho pensato che potesse aiutare qualcun altro; sicuramente se si desidera ottenere lo stesso comportamento per una cella di visualizzazione tabella.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top