Created
August 7, 2013 04:58
-
-
Save kleneway/6171311 to your computer and use it in GitHub Desktop.
UIPanGestureRecognizer for creating parallax effect on an image view with text view
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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