Skip to content

Instantly share code, notes, and snippets.

@kleneway
Created August 7, 2013 04:58
Show Gist options
  • Save kleneway/6171311 to your computer and use it in GitHub Desktop.
Save kleneway/6171311 to your computer and use it in GitHub Desktop.
UIPanGestureRecognizer for creating parallax effect on an image view with text view
- (IBAction)handlePanGesture:(UIPanGestureRecognizer*)gesture
{
CGPoint translation = [gesture translationInView:self.view];
if (gesture.state == UIGestureRecognizerStateChanged) {
// user is swiping, translate the view for the image by 1/4 the translation, and the text by 1/2 the translation to give parallax effect
self.currentSlide.imageView.frame = CGRectMake(self.currentSlide.imageView.frame.origin.x+translation.x/4,0,self.currentSlide.imageView.frame.size.width, self.currentSlide.imageView.frame.size.height);
self.currentSlide.slideText.view.frame = CGRectMake(self.currentSlide.slideText.view.frame.origin.x+translation.x/2,0,self.currentSlide.slideText.view.frame.size.width, self.currentSlide.slideText.view.frame.size.height);
// save the cumulative translation and reset the local translation
self.panTranslation += translation.x;
[gesture setTranslation:CGPointZero inView:self.view];
}
if (gesture.state == UIGestureRecognizerStateCancelled || gesture.state == UIGestureRecognizerStateFailed)
{
// gesture was interrupted, reset the view and clear the translation
[self resetSlideView];
self.panTranslation = 0;
}
if (gesture.state == UIGestureRecognizerStateEnded)
{
// gesture finished. If swipe is past threshold, perform left or right swipe animation. Otherwise, reset the view.
CGPoint velocity = [gesture velocityInView:self.view];
if(abs(self.panTranslation) > (self.view.bounds.size.width*.15)) {
if(velocity.x < 0) {
[self swipeLeft];
} else {
[self swipeRight];
}
} else {
[self resetSlideView];
}
self.panTranslation = 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment