Skip to content

Instantly share code, notes, and snippets.

@jhays
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhays/11183614 to your computer and use it in GitHub Desktop.
Save jhays/11183614 to your computer and use it in GitHub Desktop.
UIView+Parallax Category For Easy Parallax and Shadow Movement
//
// UIView+Parallax.h
// Orbosphere
//
// Created by Julian Hays on 4/22/14.
// Inspired By Ash Furrow - http://www.teehanlax.com/blog/introduction-to-uimotioneffect/ - September 19,2013
// and Michael Burford - http://michael.burford.net/2013/10/ios7-parallax-shadows.html - Thursday, October 10, 2013
//
//
#import <UIKit/UIKit.h>
@interface UIView (Parallax)
- (void)addShadowing;
- (void)addShadowParallaxMotionEffect:(CGFloat)amount;
- (void)addParallaxMotionEffect:(CGFloat)amount;
@end
//
// UIView+Parallax.m
// Orbosphere
//
// Created by Julian Hays on 4/22/14.
// Inspired By Ash Furrow - http://www.teehanlax.com/blog/introduction-to-uimotioneffect/ - September 19,2013
// and Michael Burford - http://michael.burford.net/2013/10/ios7-parallax-shadows.html - Thursday, October 10, 2013
//
//
#import "UIView+Parallax.h"
@implementation UIView (Parallax)
- (void)addShadowing {
[self.layer setShadowColor:[UIColor blackColor].CGColor];
[self.layer setShadowOpacity:0.4];
[self.layer setShadowRadius:3.0];
[self.layer setShadowOffset:CGSizeMake(0, 2)];
}
- (void)addShadowParallaxMotionEffect:(CGFloat)amount {
amount = 10.0*amount;
UIInterpolatingMotionEffect *motionX = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"layer.shadowOffset.width" type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
[motionX setMaximumRelativeValue:@(amount)];
[motionX setMinimumRelativeValue:@(-amount)];
UIInterpolatingMotionEffect *motionY = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"layer.shadowOffset.height" type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];
[motionY setMaximumRelativeValue:@(amount)];
[motionY setMinimumRelativeValue:@(-amount)];
UIMotionEffectGroup *group = [[UIMotionEffectGroup alloc] init];
group.motionEffects = @[motionX, motionY];
[self addMotionEffect:group];
}
- (void)addParallaxMotionEffect:(CGFloat)amount
{
amount = 10.0*amount;
UIInterpolatingMotionEffect *verticalMotionEffect = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.y" type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];
verticalMotionEffect.minimumRelativeValue = @(-amount);
verticalMotionEffect.maximumRelativeValue = @(amount);
UIInterpolatingMotionEffect *horizontalMotionEffect = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.x" type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
horizontalMotionEffect.minimumRelativeValue = @(-amount);
horizontalMotionEffect.maximumRelativeValue = @(amount);
UIMotionEffectGroup *group = [UIMotionEffectGroup new];
group.motionEffects = @[horizontalMotionEffect, verticalMotionEffect];
[self addMotionEffect:group];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment