ios - MKAnnotation needs single tap before long-tap draggable -
i’ve read lot mkannotation , how need implement setcoordinate in subclass draggable=true in order make whole shebang draggable.
my situation in ios7-only app, annotation draggable no matter whether implement setcoordinate or not…but problem need tap first (which pops out callout accessory) , long tap it, , hover in air above map , can dragged. confusing user because it’s different how in standard maps app. notice in maps app long tap on annotation make hover & draggable without prerequisite tap.
i’ve tried implementing setcoordinate, doesn’t make difference. other annotation subclass stores latitude & longitude, works fine. want draggable straight away on long tap.
relevant code view controller implements mkmapviewdelegate. can verify putting breakpoints in delegate methods.
- (void)viewdidload { [super viewdidload]; [mapview setdelegate:self]; } -(mkannotationview *)mapview:(mkmapview *)mv viewforannotation: (id <mkannotation>)annotation { mkpinannotationview *pinview = nil; if(annotation != mapview.userlocation) { static nsstring *defaultpinid = @"pointpin"; pinview = (mkpinannotationview *)[mapview dequeuereusableannotationviewwithidentifier:defaultpinid]; if ( pinview == nil ) { pinview = [[mkpinannotationview alloc] initwithannotation:annotation reuseidentifier:defaultpinid]; } pinview.pincolor = mkpinannotationcolorred; if ([annotation iskindofclass:[simplemapannotation class]]) { simplemapannotation *simplemapannotation = (simplemapannotation*)annotation; if ([simplemapannotation color]) { pinview.pincolor = [simplemapannotation color]; } if (simplemapannotation.moveable) { pinview.draggable=true; // delete button remove annotation uibutton *button = [uibutton buttonwithtype:uibuttontypecustom]; [button setimage:[[uiimage alloc] initwithcontentsoffile:[[nsbundle mainbundle] pathforresource:@"trash" oftype:@"png"]] forstate:uicontrolstatenormal] ; button.frame = cgrectmake(0, 0, 23, 23); pinview.rightcalloutaccessoryview = button; } } pinview.canshowcallout = yes; pinview.animatesdrop = yes; } else { [mapview.userlocation settitle:@"i here"]; } return pinview; } - (void)mapview:(mkmapview *)themapview annotationview:(mkannotationview *)view calloutaccessorycontroltapped:(uicontrol *)control{ if([view.annotation iskindofclass:[simplemapannotation class]]){ simplemapannotation *annotation = (simplemapannotation*)view.annotation; //remove point database //<snip> [uiview animatewithduration:0.3 delay:0.0 options:0 animations:(void (^)(void)) ^{ //remove annotation map view.alpha = 0.0f; } completion:^(bool finished){ [themapview removeannotation:annotation]; view.alpha=1.0f; }]; } } - (void)mapview:(mkmapview *)mapview annotationview:(mkannotationview *)annotationview didchangedragstate:(mkannotationviewdragstate)newstate fromoldstate:(mkannotationviewdragstate)oldstate { if (newstate == mkannotationviewdragstateending) { if ([annotationview.annotation ismemberofclass:[simplemapannotation class]]) { simplemapannotation *simplemapannotation = (simplemapannotation*)annotationview.annotation; simplemapannotation.latitude = [nsnumber numberwithdouble:simplemapannotation.coordinate.latitude]; simplemapannotation.longitude = [nsnumber numberwithdouble:simplemapannotation.coordinate.longitude]; } cllocationcoordinate2d droppedat = annotationview.annotation.coordinate; nslog(@"dropped @ %f,%f", droppedat.latitude, droppedat.longitude); } }
to begin drag of mkannotationview object should selected first. it's design.
if want start moving of annotation view on long tap should set selected property yes before touches of long tap have been delivered object.
to make successor of mkpinannotationview class following:
// mkimmideatedragpinannotationview.h @interface mkimmideatedragpinannotationview : mkpinannotationview @end // mkimmideatedragpinannotationview.m @implementation mkimmideatedragpinannotationview - (void)touchesbegan:(nsset *)touches withevent:(uievent *)event { [self setselected:yes]; [super touchesbegan:touches withevent:event]; } @end then change mkpinannotationview class mkimmideatedragpinannotationview class @ pinview allocation in code:
-(mkannotationview *)mapview:(mkmapview *)mv viewforannotation:(id <mkannotation>)annotation { ... if ( pinview == nil ) { pinview = [[mkimmideatedragpinannotationview alloc] initwithannotation:annotation reuseidentifier:defaultpinid]; } ... } your pin start drag on long tap. , show callout on single tap, usual.
that trick work mkannotationview class in ios 6.xx - ios 7.xx.
Comments
Post a Comment