Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mlabraca/2e21c0e7dcaa17f092fc to your computer and use it in GitHub Desktop.
Save mlabraca/2e21c0e7dcaa17f092fc to your computer and use it in GitHub Desktop.
UIInterpolatingMotionEffect vertically and horizontally for a view (iOS 7 and later)
+ (void) applyMotionEffects:(UIView *)view forMotionEffectExtent:(float)motionEffectExtentOffset
{
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
return;
}
UIInterpolatingMotionEffect *horizontalEffect = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.x"
type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
horizontalEffect.minimumRelativeValue = @(-motionEffectExtentOffset);
horizontalEffect.maximumRelativeValue = @( motionEffectExtentOffset);
UIInterpolatingMotionEffect *verticalEffect = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.y"
type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];
verticalEffect.minimumRelativeValue = @(-motionEffectExtentOffset);
verticalEffect.maximumRelativeValue = @( motionEffectExtentOffset);
UIMotionEffectGroup *motionEffectGroup = [[UIMotionEffectGroup alloc] init];
motionEffectGroup.motionEffects = @[horizontalEffect, verticalEffect];
[view addMotionEffect:motionEffectGroup];
[horizontalEffect release];
[verticalEffect release];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment