Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Easy UIInterpolatingMotionEffect. Learn more: http://hack.swic.name/easy-uiinterpolatingmotioneffect
//
// NaturalMotion.swift
//
// Created by Maciej Swic on 2014-06-06.
// Released under the MIT license.
//
import UIKit
extension UIView {
func addNaturalOnTopEffect(maximumRelativeValue : Float = 20.0) {
//Horizontal motion
var motionEffect = UIInterpolatingMotionEffect(keyPath: "center.x", type: .TiltAlongHorizontalAxis);
motionEffect.minimumRelativeValue = maximumRelativeValue;
motionEffect.maximumRelativeValue = -maximumRelativeValue;
addMotionEffect(motionEffect);
//Vertical motion
motionEffect = UIInterpolatingMotionEffect(keyPath: "center.y", type: .TiltAlongVerticalAxis);
motionEffect.minimumRelativeValue = maximumRelativeValue;
motionEffect.maximumRelativeValue = -maximumRelativeValue;
addMotionEffect(motionEffect);
}
func addNaturalBelowEffect(maximumRelativeValue : Float = 20.0) {
addNaturalOnTopEffect(maximumRelativeValue: -maximumRelativeValue)
}
}
//
// UIView+NaturalMotion.h
//
// Created by Maciej Swic on 30/04/14.
// Released under the MIT license.
//
#import <UIKit/UIKit.h>
@interface UIView (NaturalMotion)
+ (void)addNaturalOnTopEffectWithMaximumRelativeValue:(CGFloat)maximumRealtiveValue;
+ (void)addNaturalBelowEffectWithMaximumRelativeValue:(CGFloat)maximumRealtiveValue;
@end
//
// UIView+NaturalMotion.m
//
// Created by Maciej Swic on 30/04/14.
// Released under the MIT license.
//
#import "UIView+NaturalMotion.h"
@implementation UIView (NaturalMotion)
+ (void)addNaturalOnTopEffectWithMaximumRelativeValue:(CGFloat)maximumRealtiveValue {
UIInterpolatingMotionEffect* motionEffect = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.x" type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
motionEffect.minimumRelativeValue = @(maximumRealtiveValue);
motionEffect.maximumRelativeValue = @(-maximumRealtiveValue);
[self addMotionEffect:motionEffect];
motionEffect = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.y" type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];
motionEffect.minimumRelativeValue = @(maximumRealtiveValue);
motionEffect.maximumRelativeValue = @(-maximumRealtiveValue);
[self addMotionEffect:motionEffect];
}
+ (void)addNaturalBelowEffectWithMaximumRelativeValue:(CGFloat)maximumRealtiveValue {
UIInterpolatingMotionEffect* motionEffect = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.x" type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
motionEffect.minimumRelativeValue = @(-maximumRealtiveValue);
motionEffect.maximumRelativeValue = @(maximumRealtiveValue);
[self addMotionEffect:motionEffect];
motionEffect = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.y" type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];
motionEffect.minimumRelativeValue = @(-maximumRealtiveValue);
motionEffect.maximumRelativeValue = @(maximumRealtiveValue);
[self addMotionEffect:motionEffect];
}
@end
@smileyborg

This comment has been minimized.

Show comment Hide comment
@smileyborg

smileyborg May 6, 2014

Why not just make this a category on UIView? That would reduce the API down to something like:

@interface UIView (NaturalMotion)

- (void)addNaturalMotionEffectAboveWithMaximumRelativeValue:(CGFloat)maximumRelativeValue;
- (void)addNaturalMotionEffectBelowWithMaximumRelativeValue:(CGFloat)maximumRelativeValue;

@end

Why not just make this a category on UIView? That would reduce the API down to something like:

@interface UIView (NaturalMotion)

- (void)addNaturalMotionEffectAboveWithMaximumRelativeValue:(CGFloat)maximumRelativeValue;
- (void)addNaturalMotionEffectBelowWithMaximumRelativeValue:(CGFloat)maximumRelativeValue;

@end
@maciekish

This comment has been minimized.

Show comment Hide comment
@maciekish

maciekish Jun 6, 2014

Good idea! I also added a Swift version.

Owner

maciekish commented Jun 6, 2014

Good idea! I also added a Swift version.

@khoogheem

This comment has been minimized.

Show comment Hide comment
@khoogheem

khoogheem Nov 9, 2014

Remove Effects-

func removeMotionEffects() {
    let motionEffects = self.motionEffects as [UIMotionEffect]
    for effect in motionEffects {
        self.removeMotionEffect(effect)
    }
}
- (void)removeMotionEffects {
    NSArray *motionEffects = [NSArray arrayWithArray:self.motionEffects];
    [motionEffects enumerateObjectsUsingBlock:^(UIMotionEffect *motionEffect, NSUInteger idx, BOOL *stop) {
        [self removeMotionEffect:motionEffect];
    }];

}

Remove Effects-

func removeMotionEffects() {
    let motionEffects = self.motionEffects as [UIMotionEffect]
    for effect in motionEffects {
        self.removeMotionEffect(effect)
    }
}
- (void)removeMotionEffects {
    NSArray *motionEffects = [NSArray arrayWithArray:self.motionEffects];
    [motionEffects enumerateObjectsUsingBlock:^(UIMotionEffect *motionEffect, NSUInteger idx, BOOL *stop) {
        [self removeMotionEffect:motionEffect];
    }];

}
@adamcian

This comment has been minimized.

Show comment Hide comment
@adamcian

adamcian Dec 18, 2014

Should group the horizontal and vertical effects into a UIMotionEffectGroup for performance so they are evaluated at the same time.

Should group the horizontal and vertical effects into a UIMotionEffectGroup for performance so they are evaluated at the same time.

@jowie

This comment has been minimized.

Show comment Hide comment
@jowie

jowie Aug 8, 2016

Left-right and top-bottom are inverted. The image should move from right-to-left if in the background.

jowie commented Aug 8, 2016

Left-right and top-bottom are inverted. The image should move from right-to-left if in the background.

@revolter

This comment has been minimized.

Show comment Hide comment
@revolter

revolter Mar 14, 2017

Typo: realtive instead of relative.

Typo: realtive instead of relative.

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