Skip to content

Instantly share code, notes, and snippets.

@ajjames
Last active August 9, 2016 14:45
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ajjames/8659136 to your computer and use it in GitHub Desktop.
Save ajjames/8659136 to your computer and use it in GitHub Desktop.
//
// UIView+Parallax.h
//
#import <Foundation/Foundation.h>
@interface UIView (Parallax)
-(void)addBackgroundParallax:(CGFloat)strength;
-(void)addForegroundParallax:(CGFloat)strength;
-(void)removeParallax;
@end
//
// UIView+Parallax.m
//
#import <objc/runtime.h>
#import "UIView+Parallax.h"
@implementation UIView (Parallax)
-(void)addBackgroundParallax:(CGFloat)strength
{
[self addParallax:-strength];
}
-(void)addForegroundParallax:(CGFloat)strength
{
[self addParallax:strength];
}
-(void)removeParallax
{
for (UIMotionEffect *effect in self.motionEffects) {
[self removeMotionEffect:effect];
}
}
-(void)addParallax:(CGFloat)amount
{
[self removeParallax];
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
//
// UIViewExtension.swift
//
import Foundation
import UIKit
public extension UIView
{
public func addBackgroundParallax(strength:CGFloat)
{
addParallax(-strength)
}
public func addForegroundParallax(strength:CGFloat)
{
addParallax(strength)
}
public func removeParallax()
{
if let effects: [UIMotionEffect] = self.motionEffects as? [UIMotionEffect]
{
for effect:UIMotionEffect in effects
{
removeMotionEffect(effect)
}
}
}
public func addParallax(amount:CGFloat)
{
removeParallax()
var verticalMotionEffect:UIInterpolatingMotionEffect = UIInterpolatingMotionEffect(keyPath: "center.y", type: UIInterpolatingMotionEffectType.TiltAlongVerticalAxis)
verticalMotionEffect.minimumRelativeValue = -amount
verticalMotionEffect.maximumRelativeValue = amount
var horizontalMotionEffect = UIInterpolatingMotionEffect(keyPath: "center.x", type: UIInterpolatingMotionEffectType.TiltAlongHorizontalAxis)
horizontalMotionEffect.minimumRelativeValue = -amount
horizontalMotionEffect.maximumRelativeValue = amount
var group = UIMotionEffectGroup()
group.motionEffects = [horizontalMotionEffect, verticalMotionEffect]
self.addMotionEffect(group)
}
}
@ajjames
Copy link
Author

ajjames commented Jan 27, 2014

Add/remove parallax effect on a view.

@ded
Copy link

ded commented Jul 12, 2015

thanks! that's a pretty neat effect.

any ideas on how you could implement (in Swift) a version that moves image on scroll of a tableView?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment