iphone - iOS panGesture with images from NSArray -
i everyone,
i have nsarray images , wan't able pass 1 image other panning left or right (just in iphone photo library). array of pictures come collection view. first displayed image 1 selected in collection view.
for now, can display right image , move image left or right don't know how display other images , when image more 50% in view moves center.
thank's lot.
here's code :
- (void)viewdidload { [super viewdidload]; //nslog(@"%i", _indexphoto); self.photoimageview.image = [uiimage imagenamed:[_photoscollectionarray objectatindex:_indexphoto]]; uipangesturerecognizer *recognizer; recognizer = [[uipangesturerecognizer alloc]initwithtarget: self action: @selector(pangesturehandle:)]; [recognizer setminimumnumberoftouches:1]; [recognizer setmaximumnumberoftouches:1]; [[self view] addgesturerecognizer:recognizer]; } -(void) pangesturehandle:(uipangesturerecognizer *) sender { nslog(@"pan gesture handling"); uipangesturerecognizer *pangesture = (uipangesturerecognizer *)sender; cgpoint translation = [pangesture translationinview:self.view]; cgpoint imageviewposition = _photoimageview.center; imageviewposition.x += translation.x; _photoimageview.center = imageviewposition; [pangesture settranslation:cgpointmake(0, 0) inview:self.view]; }
a uiswipegesturerecognizer
not right 1 use, since discrete gesture , message associated sent once.
what need uipangesturerecognizer
: allow track finger movement , animate image accordingly.
this basic example of how gesture handling done:
- (ibaction)handlepan:(uipangesturerecognizer *)recognizer { cgpoint translation = [recognizer translationinview:self.view]; recognizer.view.center = cgpointmake(recognizer.view.center.x + translation.x, recognizer.view.center.y + translation.y); [recognizer settranslation:cgpointmake(0, 0) inview:self.view]; }
edit:
to able show other image next 1 panning around, need following:
add 3 images (say, previmage, curimage, nextimage) view displayed 1 next other;
when pan around curimage, move other ones;
when curimage displacement exceeds 50% of current view, animate off screen (and animate next/previmage displayed).
you handle 3 images using uiimageview
or calayer
.
this basic scheme fine if prefer working within uiviewcontroller
. might think of having custom uiview
holding 3 images (inside of calayers
) , juggling them around in layoutsubviews
.
in case, since suppose have more 3 images, need logics set of 3 images dealing updated when move next image. add 4th point above listed ones:
after moving next (previous) image:
4.1 set previmage (nextimage) points curimage;
4.2 set curimage point nextimage (previmage);
4.3 make nextimage (previimage) point next (previous) image in array.
hope helps.
Comments
Post a Comment