iphone - Image dragging on a UIScrollView -


a uiviewcontroller called when user touches image on uitableviewcell. called using modalviewcontroller. on modalviewcontroller uiscrollview, uiimageview in middle, fetches image using nsdictionary (passed uitableviewcell).

now, wanted achieve ability user drag image vertically only, such dragging , releasing little cause image return center animation. if user drags extremes, entire uiscrollview dismissed , user returns uitableview. used following code. issue here is, , name suggests, code crude. there elegant way of doing this, without need of calculation?

bool imagemoved=no;  - (void) touchesmoved:(nsset *)touches         withevent:(uievent *)event { uitouch * touch = [touches anyobject]; cgpoint pos = [touch locationinview: [uiapplication sharedapplication].keywindow];  cgrect imagerect = _photoimageview.frame;//_photoimageview object of uiimageview cgfloat imageheight = imagerect.size.height;//getting height of image cgfloat imagetop=240-imageheight/2; cgfloat imagebottom=imagetop+imageheight;//setting boundaries getting coordinates of touch. // touches originate above imagetop , below imagebottom ignored(until touch reaches uiimageview)  if (pos.y>50&&pos.y<430&&pos.y>=imagetop&&pos.y<=imagebottom){//extremes defined top , bottom 50 pixels.     imagedmoved=yes;//bool variable hold if image dragged or not     nslog(@"%f", pos.y);     [uiview setanimationdelay:0];      [uiview animatewithduration:0.4                       animations:^{_photoimageview.frame=cgrectmake(0,pos.y-imageheight/2,320,200);}                       completion:^(bool finished){ }]; } }  - (void) touchesended:(nsset *)touches withevent:(uievent *)event {  uitouch * touch = [touches anyobject]; cgpoint pos = [touch locationinview: [uiapplication sharedapplication].keywindow];  if (pos.y>50&&pos.y<430){//if touch has not ended in extreme 50 pixels vertically     [uiview setanimationdelay:0];//restoring uiimageview original center animation      [uiview animatewithduration:0.4                       animations:^{_photoimageview.frame=cgrectmake(0,140,320,200);}                       completion:^(bool finished){ }]; imagedmoved=no;//prepare bool value next touch event }  else if(pos.y<50||pos.y>430)     if(imagedmoved)     [self.photobrowser exit] ;//exit function(imported uiscrollviewcontroller) dismisses                   //modalview using [self dismissviewcontrolleranimated:yes completion:nil]; } 

all code here modification onto customized copy of uitapview in mwphotobrowser.

yo, here easier way this, more or less example of alessandro stated. i'm not finding top of screen i'm giving illusion of it.

bcviewcontroller.h

#import <uikit/uikit.h>  @interface bcviewcontroller : uiviewcontroller <uiscrollviewdelegate> @property (weak, nonatomic) iboutlet uiscrollview *svscroller; @property (weak, nonatomic) iboutlet uiimageview *ivfish;  @end     #import "bcviewcontroller.h"  @interface bcviewcontroller (){    uipangesturerecognizer *_pangesturerecognizer;    cgpoint                 _fishorigin; }  @end  @implementation bcviewcontroller  - (void)viewdidload {     [super viewdidload];    self.svscroller.delegate = self;    [self.svscroller setcontentsize:self.view.frame.size];    self.svscroller.translatesautoresizingmaskintoconstraints = no;    self.ivfish.userinteractionenabled = yes;     _pangesturerecognizer = [[uipangesturerecognizer alloc] initwithtarget:self action:@selector(handlepanfrom:)];    [self.ivfish addgesturerecognizer:_pangesturerecognizer];     _fishorigin = self.ivfish.center;     nslog(@"center %f", self.ivfish.center.x); }  - (void)didreceivememorywarning {     [super didreceivememorywarning];     // dispose of resources can recreated. }  -(void)handlepanfrom:(uipangesturerecognizer *)recognizer{    // if want not used    cgpoint translation = [recognizer translationinview:recognizer.view];    // if want not used    cgpoint velocity    = [recognizer velocityinview:recognizer.view];    cgpoint temppoint;   if (recognizer.state == uigesturerecognizerstatebegan) {  } else if (recognizer.state == uigesturerecognizerstatechanged) {     temppoint = [recognizer locationinview:self.view];      self.ivfish.center = cgpointmake(175.5, temppoint.y);  } else if (recognizer.state == uigesturerecognizerstateended) {     cgpoint temppoint;     temppoint = [recognizer locationinview:self.view];      if (temppoint.y < 132) {         [uiview animatewithduration:.3 delay:0                             options:uiviewanimationoptioncurveeaseout                          animations:^ {                              [self.navigationcontroller popviewcontrolleranimated:true];                          }                          completion:null];     } else {         [uiview animatewithduration:.3 delay:0                             options:uiviewanimationoptioncurveeaseout                          animations:^ {                              self.ivfish.center = _fishorigin;                          }                          completion:null];     } } } 

Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -