iphone - How to Drag a UIImageView? -


here situation: have 3 uiimageviews. 2 of them act reactors (the ones third 1 dragged to). third one, mentioned, draggable 1 of 2 uiimageviews mentioned before.

i using touchesbegan, touchesmoved, , touchesended accomplish this, there 1 problem.

after draggable uiimageview has been dragged 1 of 2 uiimageviews, want draggable image view snap (animation) original position (bottom center).

how accomplish this? in advance!

i tried out in miniproject dragging-snapping using more convenient uipangesturerecognizer. hope solve problem, comment if need more info (you can download github project https://github.com/codedad/so_drag_n_snap_ios ).

my viewcontroller.h:

@interface viewcontroller : uiviewcontroller <uigesturerecognizerdelegate> {     cgpoint firstpos; }  @property iboutlet uiimageview *spot1; @property iboutlet uiimageview *spot2; @property iboutlet uiimageview *bkgr; @property iboutlet uiimageview *dragimg;  - (float) distancesquared:(cgpoint) p1 p2:(cgpoint) p2; - (ibaction)pan:(uipangesturerecognizer *)gesture; 

my viewcontroller.m:

- (float) distancesquared:(cgpoint) p1 p2:(cgpoint) p2 {     float dx = (p1.x-p2.x);     float dy = (p1.y-p2.y);     return dx*dx+dy*dy; }  void sort(int*a,int n,int*ndx) {     int*b,*c,t;     int*bi,*ci,ti;     for(b=a+n,bi=ndx+n ; --b>a;) {         --bi;         for(c=b,ci=bi;--c>=a;) {             --ci;             if(*c>*b)t=*b,*b=*c,*c=t,ti=*bi,*bi=*ci,*ci=ti;         }     } }  - (ibaction)pan:(uipangesturerecognizer *)gesture {     cgpoint translatedpoint = [gesture translationinview:self.view];      if ([gesture state] == uigesturerecognizerstatebegan) {         firstpos = [[gesture view] center];     }      translatedpoint = cgpointmake(firstpos.x+translatedpoint.x, firstpos.y+translatedpoint.y);     [[gesture view] setcenter:translatedpoint];      if ([gesture state] == uigesturerecognizerstateended) {         cgpoint snappos;         int distances[3];         distances[0] = [self distancesquared:dragimg.center p2:spot1.center];         distances[1] = [self distancesquared:dragimg.center p2:spot2.center];         distances[2] = [self distancesquared:dragimg.center p2:bkgr.center];         int distances_ndx[3] = {0,1,2};         sort(distances, 3, distances_ndx);          switch (distances_ndx[0]) {             case 0:                 snappos = spot1.center;                 break;             case 1:                 snappos = spot2.center;                 break;             default:                 snappos = bkgr.center;                 break;         }         [uiview beginanimations:nil context:null];         [uiview setanimationduration:0.2];         [uiview setanimationcurve:uiviewanimationcurveeaseout];         [uiview setanimationdelegate:self];         dragimg.center = snappos;         [uiview commitanimations];     } } 

my viewcontroller.xib:

i included 4 uiimageviews. 3 of them static (spot1,2 , "home" spot background halftone circle), plus draggable yellow circle inside "home" spot. added pan gesture recognizer. set outlets:

  • uiimageviews connected spot1, spot2, bkgr , dragimg
  • pan gesture's delegate connected view, action connected "pan" method
  • dragimg's gesturerecognizer connected pan gesture recognizer (added viewcontroller dragging before)

enter image description here

  • you can add more spots if extend distance calculations , modify sorting properly

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -