objective c - drag and drop uiimage into another uiimageview -
i using following code snippet drag , drop uiimageview
uipangesturerecognizer *panrecognizer = [[uipangesturerecognizer alloc] initwithtarget:self action:@selector(move:)]; [panrecognizer setminimumnumberoftouches:1]; [panrecognizer setmaximumnumberoftouches:1]; [panrecognizer setdelegate:self]; [myimageview addgesturerecognizer:panrecognizer]; -(void)move:(id)sender { cgpoint translatedpoint = [(uipangesturerecognizer*)sender translationinview:self.view]; if([(uipangesturerecognizer*)sender state] == uigesturerecognizerstatebegan) { firstx = [myimageview center].x; firsty = [myimageview center].y; } translatedpoint = cgpointmake(firstx+translatedpoint.x, firsty+translatedpoint.y); [myimageview setcenter:translatedpoint]; } this code drags whole myimageview ,but requirement drag uiimage , drop uiimagview.myimageview should stay after dragging also.just need drag myimageview layer.draggable image should transparent. ideas b appreciated.
i have put little effort achieve output. try it
step 1 :define 3 variables in .h file
uiimageview *ivsource1, *ivdestination2, *tempiv; step 2 : initialize 3 uiimageview , add viewcontroller write in viewdidload method
ivsource1 = [[uiimageview alloc] initwithimage:[uiimage imagenamed:@"1.jpg"]]; [ivsource1 setframe:cgrectmake(100, 100, 100, 100)]; [ivsource1 settag:100]; [ivsource1 setuserinteractionenabled:yes]; [self.view addsubview:ivsource1]; ivdestination2 = [[uiimageview alloc] init]; [ivdestination2 setframe:cgrectmake(200, 300, 100, 100)]; [ivdestination2 settag:101]; [ivdestination2 setuserinteractionenabled:yes]; [self.view addsubview:ivdestination2]; tempiv = [[uiimageview alloc] init]; [tempiv setframe:cgrectmake(0, 300, 100, 100)]; [tempiv settag:102]; [tempiv setuserinteractionenabled:yes]; [self.view addsubview:tempiv]; step 3 : define following touch methods handle movement of image drag & drop
-(void)touchesbegan:(nsset *)touches withevent:(uievent *)event { uitouch *touch = [touches anyobject]; if([[touch view] tag] == 100) { [tempiv setimage:ivsource1.image]; [tempiv setcenter:[touch locationinview:self.view]]; } } - (void)touchesmoved:(nsset *)touches withevent:(uievent *)event { uitouch *touch = [touches anyobject]; [tempiv setcenter:[touch locationinview:self.view]]; } - (void)touchesended:(nsset *)touches withevent:(uievent *)event { uitouch *touch = [touches anyobject]; [tempiv setcenter:[touch locationinview:self.view]]; if(cgrectcontainspoint(ivdestination2.frame, [touch locationinview:self.view])) { [ivdestination2 setimage:tempiv.image]; } // remove image dragable view [tempiv setimage:[uiimage imagenamed:@""]]; }
Comments
Post a Comment