iphone - Drag and drop objects and return them to their original position -
i'm trying drag image , let return it's original position after releasing it. far, can drag image creating button using following code: (as seen in answer question: basic drag , drop in ios )
uibutton *button = [uibutton buttonwithtype:uibuttontypecustom]; [button addtarget:self action:@selector(imagetouch:withevent:) forcontrolevents:uicontroleventtouchdown]; [button addtarget:self action:@selector(imagemoved:withevent:) forcontrolevents:uicontroleventtouchdraginside]; [button setimage:[uiimage imagenamed:@"vehicle.png"] forstate:uicontrolstatenormal]; [self.view addsubview:button]; and later define:
- (ibaction) imagemoved:(id) sender withevent:(uievent *) event { cgpoint point = [[[event alltouches] anyobject] locationinview:self.view]; uicontrol *control = sender; control.center = point; } how can make return it's original position after releasing ?, starters save position @ each image supposed return, then, there anyway indicate uiimage go it's current position , move 1 ?, or other alterative solution ?
you know what, working on yesterday, here go little bonus animation:
- (void)previewimagetouchupinside:(uibutton*)abutton { if (!previewimagedragged) { [self selectpreviewimage:abutton]; return; } previewimagedragged = no; // if user drag photo little , release, we'll still treat tap // instead of drag if ((originalpositionofbutton.x - abutton.center.x) * (originalpositionofbutton.x - abutton.center.x) + (originalpositionofbutton.y - abutton.center.y) * (originalpositionofbutton.y - abutton.center.y) < 10) { abutton.center = originalpositionofbutton; [self selectpreviewimage:abutton]; return; } [uiview animatewithduration:0.5 delay:0 options:uiviewanimationoptioncurveeaseout | uiviewanimationoptionallowuserinteraction animations:^{ abutton.center = originalpositionofbutton; } completion:nil]; } - (void)previewimagedraggedinside:(uibutton*)abutton event:(uievent*)event { if (!previewimagedragged) { originalpositionofbutton = abutton.center; [self.view bringsubviewtofront:abutton]; [self.view bringsubviewtofront:[self.view viewwithtag:choose_photo_button_tag]]; } uitouch* touch = [[event alltouches] anyobject]; previewimagedragged = yes; abutton.center = cgpointmake(abutton.center.x+[touch locationinview:self.view].x- [touch previouslocationinview:self.view].x, abutton.center.y+[touch locationinview:self.view].y- [touch previouslocationinview:self.view].y); } it still has few magic numbers , haven't renamed functions , variables suit example, should clear. did indentation here since don't manually break long lines in code.
Comments
Post a Comment