Last active
August 9, 2016 14:45
-
-
Save ajjames/8659136 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// UIView+Parallax.h | |
// | |
#import <Foundation/Foundation.h> | |
@interface UIView (Parallax) | |
-(void)addBackgroundParallax:(CGFloat)strength; | |
-(void)addForegroundParallax:(CGFloat)strength; | |
-(void)removeParallax; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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) | |
} | |
} |
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
Add/remove parallax effect on a view.