Skip to content

Instantly share code, notes, and snippets.

@zapsleep
Created July 29, 2013 08:18
Show Gist options
  • Save zapsleep/6102866 to your computer and use it in GitHub Desktop.
Save zapsleep/6102866 to your computer and use it in GitHub Desktop.
Setter for ContentOffset in DVParallaxView
-(void)setContentOffset:(CGPoint)contentOffset {
BOOL backgroundReachedEdgeX = NO;
BOOL backgroundReachedEdgeY = NO;
double contentDivider;
//1
if (self.backgroundImageView) {
contentDivider = self.subviews.count*self.parallaxDistanceFactor;
CGPoint newCenter = CGPointMake(self.backgroundImageView.center.x + (contentOffset.x - _contentOffset.x)/contentDivider,
self.backgroundImageView.center.y - (contentOffset.y - _contentOffset.y)/contentDivider);
if ((newCenter.x - self.backgroundImageView.frame.size.width/2.f) > 0.f ||
(newCenter.x + self.backgroundImageView.frame.size.width/2.f) < self.bounds.size.width) {
newCenter.x = self.backgroundImageView.center.x;
backgroundReachedEdgeX = YES;
}
if ((newCenter.y - self.backgroundImageView.frame.size.height/2.f) > 0.f ||
(newCenter.y + self.backgroundImageView.frame.size.height/2.f) < self.bounds.size.height) {
newCenter.y = self.backgroundImageView.center.y;
backgroundReachedEdgeY = YES;
}
self.backgroundImageView.center = newCenter;
}
//
//2
for (int i = 1; i<self.subviews.count; ++i) {
UIView *view = [self.subviews objectAtIndex:i];
contentDivider = (view == self.frontView)?-self.parallaxFrontFactor:((self.subviews.count - i)*self.parallaxDistanceFactor);
CGFloat newCenterX = backgroundReachedEdgeX?view.center.x:(view.center.x + (contentOffset.x - _contentOffset.x)/contentDivider);
CGFloat newCenterY = backgroundReachedEdgeY?view.center.y:(view.center.y - (contentOffset.y - _contentOffset.y)/contentDivider);
view.center = CGPointMake(newCenterX, newCenterY);
}
//
_contentOffset = contentOffset;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment