I'm trying to create a Parallax effect on a UIView inside a UIScrollView. The effect seems to work, but not so well.
- First i add two UIView sub-views to a UIScrollView and set the UIScrollViews contentSize.
- The Views sum up and create a contentSize of {320, 1000}.
Then I implemented the following in scrollViewDidScroll:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView { CGFloat offsetY = scrollView.contentOffset.y; CGFloat percentage = offsetY / scrollView.contentSize.height; NSLog(@"percent = %f", percentage); if (offsetY < 0) { firstView.center = CGPointMake(firstView.center.x, firstView.center.y - percentage * 10); } else if (offsetY > 0){ firstView.center = CGPointMake(firstView.center.x, firstView.center.y + percentage * 10); } }
These lines of code do create a parallax effect, but as the scrolling continues, the view does not return to it's original position if i scroll to the original starting position.
I have tried manipulating the views layers and frame, all with the same results.
Any Help will be much appreciated.