diff --git a/APParallaxHeader/UIScrollView+APParallaxHeader.h b/APParallaxHeader/UIScrollView+APParallaxHeader.h index d379dce..c5716bb 100755 --- a/APParallaxHeader/UIScrollView+APParallaxHeader.h +++ b/APParallaxHeader/UIScrollView+APParallaxHeader.h @@ -14,9 +14,15 @@ @interface UIScrollView (APParallaxHeader) +- (void)addParallaxWithImage:(UIImage *)image portraitHeight:(CGFloat)portraitHeight landscapeHeight:(CGFloat)landscapeHeight andShadow:(BOOL)shadow; +- (void)addParallaxWithImage:(UIImage *)image heightRatio:(CGFloat)heightRatio andShadow:(BOOL)shadow; - (void)addParallaxWithImage:(UIImage *)image andHeight:(CGFloat)height andShadow:(BOOL)shadow; - (void)addParallaxWithImage:(UIImage *)image andHeight:(CGFloat)height; +- (void)addParallaxWithView:(UIView*)view portraitHeight:(CGFloat)portraitHeight landscapeHeight:(CGFloat)landscapeHeight andShadow:(BOOL)shadow; +- (void)addParallaxWithView:(UIView*)view heightRatio:(CGFloat)heightRatio andShadow:(BOOL)shadow; +- (void)addParallaxWithView:(UIView*)view andHeight:(CGFloat)height andShadow:(BOOL)shadow; - (void)addParallaxWithView:(UIView*)view andHeight:(CGFloat)height; +- (void)rotateParallaxViewToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation; @property (nonatomic, strong, readonly) APParallaxView *parallaxView; @property (nonatomic, assign) BOOL showsParallax; @@ -40,7 +46,6 @@ typedef NS_ENUM(NSUInteger, APParallaxTrackingState) { @property (nonatomic, strong) UIImageView *imageView; @property (nonatomic, strong) UIView *currentSubView; @property (nonatomic, strong) APParallaxShadowView *shadowView; -@property (nonatomic, strong) UIView *customView; - (id)initWithFrame:(CGRect)frame andShadow:(BOOL)shadow; diff --git a/APParallaxHeader/UIScrollView+APParallaxHeader.m b/APParallaxHeader/UIScrollView+APParallaxHeader.m index 57e63e1..14b5468 100755 --- a/APParallaxHeader/UIScrollView+APParallaxHeader.m +++ b/APParallaxHeader/UIScrollView+APParallaxHeader.m @@ -15,7 +15,9 @@ @interface APParallaxView () @property (nonatomic, weak) UIScrollView *scrollView; @property (nonatomic, readwrite) CGFloat originalTopInset; -@property (nonatomic) CGFloat parallaxHeight; +@property (nonatomic) CGFloat parallaxPortraitHeight; +@property (nonatomic) CGFloat parallaxLandscapeHeight; +@property (nonatomic) CGFloat heightRatio; @property(nonatomic, assign) BOOL isObserving; @@ -35,6 +37,11 @@ - (void)addParallaxWithImage:(UIImage *)image andHeight:(CGFloat)height { } - (void)addParallaxWithImage:(UIImage *)image andHeight:(CGFloat)height andShadow:(BOOL)shadow { + [self addParallaxWithImage:image portraitHeight:height landscapeHeight:height andShadow:shadow]; +} + +- (void)addParallaxWithImage:(UIImage *)image heightRatio:(CGFloat)heightRatio andShadow:(BOOL)shadow +{ if(self.parallaxView) { if(self.parallaxView.currentSubView) { [self.parallaxView.currentSubView removeFromSuperview]; @@ -43,18 +50,100 @@ - (void)addParallaxWithImage:(UIImage *)image andHeight:(CGFloat)height andShado } else { - APParallaxView *parallaxView = [[APParallaxView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width*2, height) andShadow:shadow]; + CGFloat heightByRatio = heightRatio * self.bounds.size.width; + + APParallaxView *view = [[APParallaxView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, heightRatio) andShadow:shadow]; + [view setClipsToBounds:YES]; + [view.imageView setImage:image]; + + view.scrollView = self; + view.heightRatio = heightRatio; + [self addSubview:view]; + + view.originalTopInset = self.contentInset.top; + + UIEdgeInsets newInset = self.contentInset; + newInset.top = heightByRatio; + self.contentInset = newInset; + + self.parallaxView = view; + self.showsParallax = YES; + } +} + +- (void)addParallaxWithImage:(UIImage *)image portraitHeight:(CGFloat)portraitHeight landscapeHeight:(CGFloat)landscapeHeight andShadow:(BOOL)shadow { + if(self.parallaxView) { + if(self.parallaxView.currentSubView) { + [self.parallaxView.currentSubView removeFromSuperview]; + } + [self.parallaxView.imageView setImage:image]; + } + else + { + CGFloat heightForCurrentOrientation; + if (UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) { + heightForCurrentOrientation = landscapeHeight; + } else { + heightForCurrentOrientation = portraitHeight; + } + + APParallaxView *view = [[APParallaxView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, heightForCurrentOrientation) andShadow:shadow]; + [view setClipsToBounds:YES]; + [view.imageView setImage:image]; + + view.scrollView = self; + view.parallaxLandscapeHeight = landscapeHeight; + view.parallaxPortraitHeight = portraitHeight; + view.heightRatio = -1; + [self addSubview:view]; + + view.originalTopInset = self.contentInset.top; + + UIEdgeInsets newInset = self.contentInset; + newInset.top = heightForCurrentOrientation; + self.contentInset = newInset; + + self.parallaxView = view; + self.showsParallax = YES; + } +} + +- (void)addParallaxWithView:(UIView*)view andHeight:(CGFloat)height { + [self addParallaxWithView:view andHeight:height andShadow:YES]; +} + +- (void)addParallaxWithView:(UIView*)view andHeight:(CGFloat)height andShadow:(BOOL)shadow { + [self addParallaxWithView:view portraitHeight:height landscapeHeight:height andShadow:shadow]; +} + +- (void)addParallaxWithView:(UIView *)view heightRatio:(CGFloat)heightRatio andShadow:(BOOL)shadow { + if(self.parallaxView) { + [self.parallaxView.currentSubView removeFromSuperview]; + [view setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight]; + [self.parallaxView addSubview:view]; + } + else + { + CGFloat heightByRatio = heightRatio * self.bounds.size.width; + + APParallaxView *parallaxView = [[APParallaxView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, heightByRatio) andShadow:YES]; [parallaxView setClipsToBounds:YES]; - [parallaxView.imageView setImage:image]; + [view setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight]; + [parallaxView addSubview:view]; + + // bring shadow to foreground + if (shadow) { + [parallaxView bringSubviewToFront:parallaxView.shadowView]; + } parallaxView.scrollView = self; - parallaxView.parallaxHeight = height; + parallaxView.heightRatio = heightRatio; [self addSubview:parallaxView]; parallaxView.originalTopInset = self.contentInset.top; UIEdgeInsets newInset = self.contentInset; - newInset.top = height; + newInset.top = heightByRatio; self.contentInset = newInset; self.parallaxView = parallaxView; @@ -62,27 +151,41 @@ - (void)addParallaxWithImage:(UIImage *)image andHeight:(CGFloat)height andShado } } -- (void)addParallaxWithView:(UIView*)view andHeight:(CGFloat)height { +- (void)addParallaxWithView:(UIView*)view portraitHeight:(CGFloat)portraitHeight landscapeHeight:(CGFloat)landscapeHeight andShadow:(BOOL)shadow { if(self.parallaxView) { [self.parallaxView.currentSubView removeFromSuperview]; [view setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight]; - [self.parallaxView setCustomView:view]; + [self.parallaxView addSubview:view]; } else { - APParallaxView *parallaxView = [[APParallaxView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, height)]; + CGFloat heightForCurrentOrientation; + if (UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) { + heightForCurrentOrientation = landscapeHeight; + } else { + heightForCurrentOrientation = portraitHeight; + } + + APParallaxView *parallaxView = [[APParallaxView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, heightForCurrentOrientation) andShadow:YES]; [parallaxView setClipsToBounds:YES]; + [view setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight]; + [parallaxView addSubview:view]; - [parallaxView setCustomView:view]; + // bring shadow to foreground + if (shadow) { + [parallaxView bringSubviewToFront:parallaxView.shadowView]; + } parallaxView.scrollView = self; - parallaxView.parallaxHeight = height; + parallaxView.parallaxLandscapeHeight = landscapeHeight; + parallaxView.parallaxPortraitHeight = portraitHeight; + parallaxView.heightRatio = -1; [self addSubview:parallaxView]; - + parallaxView.originalTopInset = self.contentInset.top; UIEdgeInsets newInset = self.contentInset; - newInset.top = height; + newInset.top = heightForCurrentOrientation; self.contentInset = newInset; self.parallaxView = parallaxView; @@ -90,6 +193,37 @@ - (void)addParallaxWithView:(UIView*)view andHeight:(CGFloat)height { } } +- (void)rotateParallaxViewToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { + if (self.parallaxView) { + if (self.parallaxView.heightRatio > 0) { + CGFloat heightByRatio = self.bounds.size.width * self.parallaxView.heightRatio; + + CGRect frame = self.parallaxView.frame; + frame.size.height = heightByRatio; + self.parallaxView.frame = frame; + + UIEdgeInsets newInset = self.contentInset; + newInset.top = heightByRatio; + self.contentInset = newInset; + } else { + CGFloat heightForCurrentOrientation; + if (UIInterfaceOrientationIsLandscape(interfaceOrientation)) { + heightForCurrentOrientation = self.parallaxView.parallaxLandscapeHeight; + } else { + heightForCurrentOrientation = self.parallaxView.parallaxPortraitHeight; + } + + CGRect frame = self.parallaxView.frame; + frame.size.height = heightForCurrentOrientation; + self.parallaxView.frame = frame; + + UIEdgeInsets newInset = self.contentInset; + newInset.top = heightForCurrentOrientation; + self.contentInset = newInset; + } + } +} + - (void)setParallaxView:(APParallaxView *)parallaxView { objc_setAssociatedObject(self, &UIScrollViewParallaxView, parallaxView, @@ -183,27 +317,21 @@ - (id)initWithFrame:(CGRect)frame { - (id)initWithFrame:(CGRect)frame andShadow:(BOOL)shadow { if(self = [super initWithFrame:frame]) { - [self setBackgroundColor:[UIColor redColor]]; - // default styling values - [self setAutoresizingMask:UIViewAutoresizingFlexibleHeight]; + [self setAutoresizingMask:UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth]; [self setState:APParallaxTrackingActive]; + [self setAutoresizesSubviews:YES]; - self.imageView = [[UIImageView alloc] init]; + self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(frame), CGRectGetHeight(frame))]; + [self.imageView setAutoresizingMask:UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth]; [self.imageView setContentMode:UIViewContentModeScaleAspectFill]; [self.imageView setClipsToBounds:YES]; [self addSubview:self.imageView]; - [self.imageView setTranslatesAutoresizingMaskIntoConstraints:NO]; - [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[imageView]|" options:0 metrics:nil views:@{@"imageView" : self.imageView}]]; - [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[imageView]|" options:0 metrics:nil views:@{@"imageView" : self.imageView}]]; - if (shadow) { - self.shadowView = [[APParallaxShadowView alloc] init]; + self.shadowView = [[APParallaxShadowView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(frame)-8, CGRectGetWidth(frame), 8)]; + [self.shadowView setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleWidth]; [self addSubview:self.shadowView]; - [self.shadowView setTranslatesAutoresizingMaskIntoConstraints:NO]; - [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[shadowView(8.0)]|" options:NSLayoutFormatAlignAllBottom metrics:nil views:@{@"shadowView" : self.shadowView}]]; - [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[shadowView]|" options:0 metrics:nil views:@{@"shadowView" : self.shadowView}]]; } } @@ -229,38 +357,13 @@ - (void)addSubview:(UIView *)view { self.currentSubView = view; } -- (void)setCustomView:(UIView *)customView -{ - if (_customView) { - [_customView removeFromSuperview]; - } - - _customView = customView; - - [self addSubview:customView]; - [customView setTranslatesAutoresizingMaskIntoConstraints:NO]; - [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[customView]|" options:0 metrics:nil views:@{@"customView" : customView}]]; - [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[customView]|" options:0 metrics:nil views:@{@"customView" : customView}]]; -} - -- (void)layoutSubviews -{ - [super layoutSubviews]; - - if (self.shadowView) { - [self bringSubviewToFront:self.shadowView]; - } -} - #pragma mark - Observing - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { - if([keyPath isEqualToString:@"contentOffset"]) { + if([keyPath isEqualToString:@"contentOffset"]) [self scrollViewDidScroll:[[change valueForKey:NSKeyValueChangeNewKey] CGPointValue]]; - } - else if([keyPath isEqualToString:@"frame"]) { + else if([keyPath isEqualToString:@"frame"]) [self layoutSubviews]; - } } - (void)scrollViewDidScroll:(CGPoint)contentOffset {