Skip to content

Instantly share code, notes, and snippets.

@zapsleep
zapsleep / DVParallaxViewGyroDataTransition.m
Created July 29, 2013 12:07
Transition of gyroscope data to contentOffset CGPoint
#pragma mark - Gyroscope to offset
- (CGPoint)contentOffsetWithRotationRate:(CMRotationRate)rotationRate {
double xOffset = (fabs(rotationRate.y) > DV_ROTATION_THRESHOLD)?rotationRate.y*DV_ROTATION_MULTIPLIER:0.f;
double yOffset = (fabs(rotationRate.x) > DV_ROTATION_THRESHOLD)?rotationRate.x*DV_ROTATION_MULTIPLIER:0.f;
CGPoint newOffset = CGPointMake(self.contentOffset.x + xOffset,
self.contentOffset.y + yOffset);
return newOffset;
}
@zapsleep
zapsleep / DVParallaxViewGyroscopeControlSetter.m
Created July 29, 2013 12:02
Setter for gyroscopeControl of DVParallaxView
-(void)setGyroscopeControl:(BOOL)gyroscopeControl {
if (_gyroscopeControl == gyroscopeControl)
return;
_gyroscopeControl = gyroscopeControl;
if (gyroscopeControl) {
[self.motionManager startDeviceMotionUpdates];
[self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
} else {
@zapsleep
zapsleep / DVParallaxViewGyroGetters.m
Created July 29, 2013 11:51
Getters for gyroscope in DVParallaxView
-(CADisplayLink *)displayLink {
if (!_displayLink) {
_displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(displayLinkHandler)];
}
return _displayLink;
}
-(CMMotionManager *)motionManager {
if (!_motionManager) {
@zapsleep
zapsleep / GetterForParallaxView.m
Created July 29, 2013 10:16
Getter for parallaxView w/o gyro
-(DVParallaxView *)parallaxView {
if (!_parallaxView) {
_parallaxView = [[DVParallaxView alloc] initWithFrame:self.view.bounds];
[_parallaxView setBackgroundImage:[UIImage imageNamed:@"galaxy2"]];
UIImageView *earth = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"earth"]];
earth.frame = (CGRect) {.origin = CGPointMake(CGRectGetMidX(self.view.bounds) - earth.image.size.width/2.f,
CGRectGetMidY(self.view.bounds) - earth.image.size.height/2.f),
.size = earth.frame.size};
[_parallaxView addSubview:earth];
@zapsleep
zapsleep / DVParallaxViewSettersAndGetters.m
Created July 29, 2013 08:28
Setters and Getters of DVParallaxView (w/o gyro)
#pragma mark - Getters
-(UIImageView *)backgroundImageView {
if (!_backgroundImageView) {
_backgroundImageView = [[UIImageView alloc] init];
_backgroundImageView.contentMode = UIViewContentModeCenter;
_backgroundImageView.center = CGPointMake(CGRectGetMidX(self.bounds),
CGRectGetMidY(self.bounds));
}
@zapsleep
zapsleep / DVParallaxViewContentOffset.m
Created July 29, 2013 08:18
Setter for ContentOffset in DVParallaxView
-(void)setContentOffset:(CGPoint)contentOffset {
BOOL backgroundReachedEdgeX = NO;
BOOL backgroundReachedEdgeY = NO;
double contentDivider;
//1
if (self.backgroundImageView) {
contentDivider = self.subviews.count*self.parallaxDistanceFactor;
CGPoint newCenter = CGPointMake(self.backgroundImageView.center.x + (contentOffset.x - _contentOffset.x)/contentDivider,
self.backgroundImageView.center.y - (contentOffset.y - _contentOffset.y)/contentDivider);
@zapsleep
zapsleep / DVParallaxViewPanHandler.m
Created July 29, 2013 08:09
Gesture Handler for DVParallaxView
#pragma mark - Gesture handler
- (void)panHandler:(UIPanGestureRecognizer *)pan {
CGPoint translation = [pan translationInView:self];
[self setContentOffset:CGPointMake(self.contentOffset.x + translation.x,
self.contentOffset.y - translation.y)];
[pan setTranslation:CGPointZero inView:self];
}
@zapsleep
zapsleep / DVParallaxViewInitWithFrame.m
Created July 29, 2013 08:00
Init method of DVParallaxView class
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.parallaxDistanceFactor = 2.f;
self.parallaxFrontFactor = 20.f;
self.backgroundColor = [UIColor clearColor];
[self addSubview:self.backgroundImageView];
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panHandler:)];
@zapsleep
zapsleep / DVParallaxViewPrivateInterface.m
Created July 29, 2013 07:54
Private interface of DVParallaxView (w/o gyro)
@interface DVParallaxView()
@property (nonatomic, strong) UIImageView *backgroundImageView;
@end
@zapsleep
zapsleep / DVParallaxViewHeader.m
Created July 29, 2013 07:49
Interface of DVParallaxView (w/o gyro)
#import <UIKit/UIKit.h>
@interface DVParallaxView : UIView
@property (nonatomic, strong) UIImage *backgroundImage;
@property (nonatomic, strong) UIView *frontView;
@property (nonatomic) float parallaxDistanceFactor;
@property (nonatomic) float parallaxFrontFactor;
@property (nonatomic) CGPoint contentOffset;