Permalink
Browse files

Initial commit

  • Loading branch information...
thisandagain committed Jun 1, 2012
0 parents commit 17e868c8d9c959ef7fc0edd6056787ea6695ac97
@@ -0,0 +1,10 @@
+.DS_Store
+*.swp
+*~.nib
+
+build/
+
+.xcodeproj/
+!*.xcodeproj/project.pbxproj
+*.mode1v3
+*.mode2v3
@@ -0,0 +1,21 @@
+//
+// DIYParallax.h
+// parallax
+//
+// Created by Andrew Sliwinski on 6/1/12.
+// Copyright (c) 2012 DIY, Co. All rights reserved.
+//
+
+#import <UIKit/UIKit.h>
+#import <CoreMotion/CoreMotion.h>
+
+@interface DIYParallax : UIView <UIAccelerometerDelegate>
+
+@property (nonatomic, retain) NSMutableArray *layers;
+@property (nonatomic, retain) CMMotionManager *motionManager;
+
+- (void)addLayer:(CGFloat)depth imageNamed:(NSString *)asset frame:(CGRect)frame;
+- (void)startListeningToGyro;
+- (void)stopListeningToGyro;
+
+@end
@@ -0,0 +1,151 @@
+//
+// DIYParallax.m
+// parallax
+//
+// Created by Andrew Sliwinski on 6/1/12.
+// Copyright (c) 2012 DIY, Co. All rights reserved.
+//
+
+#import "DIYParallax.h"
+
+//
+
+#define UPDATE_INTERVAL 1.0f/60
+#define DRIFT_MULTIPLIER 1.0f
+
+//
+
+@implementation DIYParallax
+
+@synthesize layers;
+@synthesize motionManager;
+
+#pragma mark - Init
+
+- (id)initWithFrame:(CGRect)frame
+{
+ self = [super initWithFrame:frame];
+ if (self) {
+ // Init layers object
+ layers = [[NSMutableArray alloc] init];
+
+ // Init motion manager
+ motionManager = [[CMMotionManager alloc] init];
+ motionManager.gyroUpdateInterval = UPDATE_INTERVAL;
+ }
+
+ return self;
+}
+
+#pragma mark - Public methods
+
+/**
+ * Add layer to view.
+ *
+ * @param int Layer index
+ * @param NSString Asset name
+ * @param CGPoint X/Y position of layer
+ *
+ * @return void
+ */
+- (void)addLayer:(CGFloat)depth imageNamed:(NSString *)asset frame:(CGRect)frame
+{
+ // Track layer
+ [layers addObject:[NSNumber numberWithFloat:depth]];
+
+ // Render to view
+ UIImageView *item = [[UIImageView alloc] initWithFrame:frame];
+ NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
+ UIImage *image = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/%@", bundlePath, asset]];
+ item.image = image;
+ [self insertSubview:item atIndex:[layers count] - 1];
+
+ [item release];
+ [image release];
+}
+
+/**
+ * Start listening to gyro updates.
+ *
+ * @return void
+ */
+- (void)startListeningToGyro
+{
+ if([self.motionManager isGyroAvailable])
+ {
+ [motionManager startGyroUpdatesToQueue:[NSOperationQueue currentQueue] withHandler: ^(CMGyroData *gyroData, NSError *error)
+ {
+ // Gather rotation rate
+ CMRotationRate rotate = gyroData.rotationRate;
+
+ // Threshold
+ CGFloat x = rotate.x;
+ CGFloat y = rotate.y;
+ x = x < -0.2 || x > 0.2 ? x : 0;
+ y = y < -0.2 || y > 0.2 ? y : 0;
+
+ // Update
+ if (x != 0 || y != 0)
+ {
+ [self update:CGPointMake(x, y)];
+ }
+ }
+ ];
+ }
+}
+
+/**
+ * Stops listening to the gyro.
+ *
+ * @return void
+ */
+- (void)stopListeningToGyro
+{
+ if([self.motionManager isGyroAvailable])
+ {
+ [motionManager stopGyroUpdates];
+ }
+ [motionManager release]; motionManager = nil;
+}
+
+#pragma mark - Private methods
+
+/**
+ * Loops through the layers array and applies subview x/y translations.
+ *
+ * @param CGPoint X/Y translation
+ *
+ * @returns void
+ */
+- (void)update:(CGPoint)point
+{
+ for (int i = 0; i < [layers count]; i++)
+ {
+ // View
+ UIView *view = [self.subviews objectAtIndex:i];
+
+ // Calc
+ CGFloat d = [[layers objectAtIndex:i] floatValue];
+ CGFloat x = view.bounds.origin.x + (point.x * d * DRIFT_MULTIPLIER);
+ CGFloat y = view.bounds.origin.y - (point.y * d * DRIFT_MULTIPLIER);
+
+ // Translate
+ view.transform = CGAffineTransformTranslate(view.transform, x, y);
+ }
+}
+
+#pragma mark - Dealloc
+
+- (void)releaseObjects
+{
+ [motionManager release]; motionManager = nil;
+ [layers release]; layers = nil;
+}
+
+- (void)dealloc
+{
+ [self releaseObjects];
+ [super dealloc];
+}
+
+@end
@@ -0,0 +1,13 @@
+Copyright 2011 DIY, Co.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
@@ -0,0 +1,43 @@
+# Parallax
+#### Objective-C library for simple implementation of CoreMotion-controlled parallax distortion.
+
+## Installation
+- Copy DIYParallax.h and DIYParallax.m into your project
+- Add the "CoreMotion" framework to your project
+
+## Getting Started
+```objective-c
+- (void)viewDidLoad
+{
+ [super viewDidLoad];
+
+ //
+
+ parallax = [[DIYParallax alloc] initWithFrame:self.view.frame];
+
+ [parallax addLayer:0.0 imageNamed:@"layer_1@2x.png" frame:CGRectMake(0, 0, 480, 320)];
+ [parallax addLayer:0.1 imageNamed:@"layer_2@2x.png" frame:CGRectMake(240, 0, 240, 320)];
+ [parallax addLayer:0.2 imageNamed:@"layer_3@2x.png" frame:CGRectMake(240, 160, 240, 160)];
+ [parallax addLayer:0.3 imageNamed:@"layer_4@2x.png" frame:CGRectMake(120, 160, 120, 160)];
+ [parallax addLayer:0.8 imageNamed:@"layer_5@2x.png" frame:CGRectMake(60, 160, 60, 160)];
+
+ [self.view addSubview:parallax];
+}
+
+- (void)viewDidAppear:(BOOL)animated
+{
+ [super viewDidAppear:animated];
+
+ [parallax startListeningToGyro];
+}
+````
+
+## Public Methods
+```
+- (void)addLayer:(CGFloat)depth imageNamed:(NSString *)asset frame:(CGRect)frame;
+- (void)startListeningToGyro;
+- (void)stopListeningToGyro;
+```
+
+## Properties
+DIYParallax is a subclass of UIView, thus all properties and methods inherent to UIView can be applied. For more information, please check the [UIView class reference](https://developer.apple.com/library/ios/#documentation/uikit/reference/uiview_class/uiview/uiview.html).
Oops, something went wrong.

0 comments on commit 17e868c

Please sign in to comment.