Skip to content

Instantly share code, notes, and snippets.

@CavalcanteLeo
Forked from ajjames/UIView+Parallax.h
Created August 9, 2016 14:45
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 CavalcanteLeo/f9a2bb71636e59c0513ad71444e288ed to your computer and use it in GitHub Desktop.
Save CavalcanteLeo/f9a2bb71636e59c0513ad71444e288ed 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)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment