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 - 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
-(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
-(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
-(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
#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; | |
} | |
OlderNewer