When we wanted to add a drag gesture to Mkmapview, the first thought might be this:
-(void) viewdidload{ //.... Uipangesturerecognizer *pangesture = [[Uipangesturerecognizer alloc] initwithtarget:self action: @selector (Handlepan :)]; [Self.mapview addgesturerecognizer:pangesture];} -(void) Handlepan: (uipangesturerecognizer*) recognizer{ NSLog ("Handlepan");}
Running the program and then dragging the map, we will find that the console does not print any "handlepan", which means that the gesture recognition handler function Handlepan has never been executed. Later on the StackOverflow found the answer, the correct solution is as follows:
-(void) viewdidload{ //... Uipangesturerecognizer *pangesture = [[Uipangesturerecognizer alloc] initwithtarget:self action: @selector (Handlepan :)]; Pangesture.delegate = self; [Self.mapview addgesturerecognizer:pangesture];} -(BOOL) Gesturerecognizer: (Uigesturerecognizer *) Gesturerecognizer Shouldrecognizesimultaneouslywithgesturerecognizer: (Uigesturerecognizer *) Othergesturerecognizer { return YES ;} -(void) Handlepan: (uipangesturerecognizer*) recognizer{ NSLog ("Handlepan");}
Compared with the previous two paragraphs, the latter program assigns the proxy to gesture recognition and implements the Shouldrecognizesimultaneouslywithgesturerecognizer proxy method.
Analyze the reason why the second code runs successfully: Mkmapview has added a uipangesturerecognizer when implemented internally, and here we have added another uipangesturerecognizer, This means that the same mkmapview has two gesture recognition of the same type, while the runtime internally defaults to the same type of gesture recognition only one will be processed, so the first piece of code always has no output handlepan. Fortunately uipangesturerecognizerdelegate provided the Gesturerecognizer:shouldrecognizesimultaneouslywithgesturerecognizer method, When this method returns Yes, it means that all gesture recognition of the same type will be processed.
iOS Development--mkmapview Add Uipangesturerecognizer