Skip to content

Instantly share code, notes, and snippets.

@wavecos
Created February 19, 2014 21:32
Show Gist options
  • Save wavecos/9102074 to your computer and use it in GitHub Desktop.
Save wavecos/9102074 to your computer and use it in GitHub Desktop.
Add Parallax Effect to View
-(void) backgroundParallaxEffect {
// Set vertical effect
UIInterpolatingMotionEffect *verticalMotionEffect =
[[UIInterpolatingMotionEffect alloc]
initWithKeyPath:@"center.y"
type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];
verticalMotionEffect.minimumRelativeValue = @(-10);
verticalMotionEffect.maximumRelativeValue = @(10);
// Set horizontal effect
UIInterpolatingMotionEffect *horizontalMotionEffect =
[[UIInterpolatingMotionEffect alloc]
initWithKeyPath:@"center.x"
type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
horizontalMotionEffect.minimumRelativeValue = @(-10);
horizontalMotionEffect.maximumRelativeValue = @(10);
// Create group to combine both
UIMotionEffectGroup *group = [UIMotionEffectGroup new];
group.motionEffects = @[horizontalMotionEffect, verticalMotionEffect];
// Add both effects to your view
// imgBackground is a UIImageView
[self.imgBackground addMotionEffect:group];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment