This file contains 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
#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; | |
} | |
This file contains 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
-(void)setGyroscopeControl:(BOOL)gyroscopeControl { | |
if (_gyroscopeControl == gyroscopeControl) | |
return; | |
_gyroscopeControl = gyroscopeControl; | |
if (gyroscopeControl) { | |
[self.motionManager startDeviceMotionUpdates]; | |
[self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; | |
} else { |
This file contains 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
-(CADisplayLink *)displayLink { | |
if (!_displayLink) { | |
_displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(displayLinkHandler)]; | |
} | |
return _displayLink; | |
} | |
-(CMMotionManager *)motionManager { | |
if (!_motionManager) { |
This file contains 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
-(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]; |
This file contains 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
#pragma mark - Getters | |
-(UIImageView *)backgroundImageView { | |
if (!_backgroundImageView) { | |
_backgroundImageView = [[UIImageView alloc] init]; | |
_backgroundImageView.contentMode = UIViewContentModeCenter; | |
_backgroundImageView.center = CGPointMake(CGRectGetMidX(self.bounds), | |
CGRectGetMidY(self.bounds)); | |
} | |
This file contains 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
-(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); |
This file contains 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
#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]; | |
} |
This file contains 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
- (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:)]; |
This file contains 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
@interface DVParallaxView() | |
@property (nonatomic, strong) UIImageView *backgroundImageView; | |
@end |
This file contains 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
#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; |
NewerOlder