Created
October 2, 2013 09:35
-
-
Save rsebbe/6791283 to your computer and use it in GitHub Desktop.
The CRParallaxMotionEffect class allows setting up parallax more easily in iOS 7.
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
// | |
// CRParallaxMotionEffect.h | |
// CeedBase | |
// | |
// Created by Raphael Sebbe on 23/07/13. | |
// BSD License (3-clause). Copyright (c) 2013 Creaceed. All rights reserved. | |
// | |
#import <UIKit/UIKit.h> | |
@interface CRParallaxMotionEffect : UIMotionEffect | |
@property (readonly) CGFloat horizontalAmplitude, verticalAmplitude; | |
// For "center" property | |
// Positive -> closer to viewer (foreground) | |
// Negative -> farther to viewer (background) | |
// Remark: the opposite for contentOffset in scrollview. | |
// default keypath is "center" | |
+ (instancetype)parallaxEffectWithAmplitude:(CGFloat)ampli; | |
+ (instancetype)parallaxEffectWithHorizontalAmplitude:(CGFloat)xampli verticalAmplitude:(CGFloat)yampli; | |
+ (instancetype)parallaxEffectWithAmplitude:(CGFloat)ampli pointKeyPath:(NSString*)pkeypath; | |
+ (instancetype)parallaxEffectWithHorizontalAmplitude:(CGFloat)xampli verticalAmplitude:(CGFloat)yampli pointKeyPath:(NSString*)pkeypath; | |
@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
// | |
// CRParallaxMotionEffect.m | |
// CeedBase | |
// | |
// Created by Raphael Sebbe on 23/07/13. | |
// BSD License (3-clause). Copyright (c) 2013 Creaceed. All rights reserved. | |
// | |
#import "CRParallaxMotionEffect.h" | |
#import "UIDevice+CeedBase.h" | |
@interface CRParallaxMotionEffect () | |
@property (readwrite) CGFloat horizontalAmplitude, verticalAmplitude; | |
@property (readwrite) NSString *pointKeyPath; | |
@end | |
@implementation CRParallaxMotionEffect | |
+ (instancetype)parallaxEffectWithAmplitude:(CGFloat)ampli | |
{ | |
return [self parallaxEffectWithHorizontalAmplitude:ampli verticalAmplitude:ampli pointKeyPath:@"center"]; | |
} | |
+ (instancetype)parallaxEffectWithAmplitude:(CGFloat)ampli pointKeyPath:(NSString*)pkeypath | |
{ | |
return [self parallaxEffectWithHorizontalAmplitude:ampli verticalAmplitude:ampli pointKeyPath:pkeypath]; | |
} | |
+ (instancetype)parallaxEffectWithHorizontalAmplitude:(CGFloat)xampli verticalAmplitude:(CGFloat)yampli | |
{ | |
return [self parallaxEffectWithHorizontalAmplitude:xampli verticalAmplitude:yampli pointKeyPath:@"center"]; | |
} | |
+ (instancetype)parallaxEffectWithHorizontalAmplitude:(CGFloat)xampli verticalAmplitude:(CGFloat)yampli pointKeyPath:(NSString*)pkeypath; | |
{ | |
CRParallaxMotionEffect *res = [[[self alloc] init] autorelease]; | |
res.horizontalAmplitude = xampli; | |
res.verticalAmplitude = yampli; | |
res.pointKeyPath = pkeypath; | |
return res; | |
} | |
- (void)dealloc | |
{ | |
self.pointKeyPath = nil; | |
} | |
- (NSDictionary *)keyPathsAndRelativeValuesForViewerOffset:(UIOffset)viewerOffset | |
{ | |
CGFloat deviceScaling = 1.0;// could reduce it for iPhone, for instance. | |
return @{ self.pointKeyPath: [NSValue valueWithCGPoint: | |
CGPointMake(viewerOffset.horizontal*self.horizontalAmplitude * deviceScaling, | |
viewerOffset.vertical*self.verticalAmplitude * deviceScaling )] | |
}; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment