diff --git a/APParallaxHeader/UIScrollView+APParallaxHeader.h b/APParallaxHeader/UIScrollView+APParallaxHeader.h index e96365e..6ee7ec8 100755 --- a/APParallaxHeader/UIScrollView+APParallaxHeader.h +++ b/APParallaxHeader/UIScrollView+APParallaxHeader.h @@ -14,10 +14,9 @@ @interface UIScrollView (APParallaxHeader) -- (void)addParallaxWithImage:(UIImage *)image andHeight:(CGFloat)height andShadow:(BOOL)shadow; +- (void)addParallaxWithImage:(UIImage *)image andHeight:(CGFloat)height andShadow:(BOOL)shadow overlap:(CGFloat)overlap; - (void)addParallaxWithImage:(UIImage *)image andHeight:(CGFloat)height; - (void)addParallaxWithView:(UIView*)view andHeight:(CGFloat)height; -- (void)addParallaxWithView:(UIView*)view andHeight:(CGFloat)height andShadow:(BOOL)shadow; @property (nonatomic, strong, readonly) APParallaxView *parallaxView; @property (nonatomic, assign) BOOL showsParallax; @@ -41,7 +40,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 b9106ea..32dff01 100755 --- a/APParallaxHeader/UIScrollView+APParallaxHeader.m +++ b/APParallaxHeader/UIScrollView+APParallaxHeader.m @@ -16,6 +16,7 @@ @interface APParallaxView () @property (nonatomic, weak) UIScrollView *scrollView; @property (nonatomic, readwrite) CGFloat originalTopInset; @property (nonatomic) CGFloat parallaxHeight; +@property (nonatomic) CGFloat overlap; @property(nonatomic, assign) BOOL isObserving; @@ -31,10 +32,10 @@ @interface APParallaxView () @implementation UIScrollView (APParallaxHeader) - (void)addParallaxWithImage:(UIImage *)image andHeight:(CGFloat)height { - [self addParallaxWithImage:image andHeight:height andShadow:YES]; + [self addParallaxWithImage:image andHeight:height andShadow:YES overlap:0]; } -- (void)addParallaxWithImage:(UIImage *)image andHeight:(CGFloat)height andShadow:(BOOL)shadow { +- (void)addParallaxWithImage:(UIImage *)image andHeight:(CGFloat)height andShadow:(BOOL)shadow overlap:(CGFloat)overlap { if(self.parallaxView) { if(self.parallaxView.currentSubView) { [self.parallaxView.currentSubView removeFromSuperview]; @@ -43,46 +44,43 @@ - (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]; - [parallaxView setClipsToBounds:YES]; - [parallaxView.imageView setImage:image]; + APParallaxView *view = [[APParallaxView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, height) andShadow:shadow]; + [view setClipsToBounds:YES]; + [view.imageView setImage:image]; - parallaxView.scrollView = self; - parallaxView.parallaxHeight = height; - [self addSubview:parallaxView]; + view.scrollView = self; + view.parallaxHeight = height; + view.overlap = overlap; + [self insertSubview:view atIndex:0]; - parallaxView.originalTopInset = self.contentInset.top; + view.originalTopInset = self.contentInset.top; UIEdgeInsets newInset = self.contentInset; - newInset.top = height; + newInset.top = height - overlap; self.contentInset = newInset; - self.parallaxView = parallaxView; + 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 { 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) andShadow:shadow]; + APParallaxView *parallaxView = [[APParallaxView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, height)]; [parallaxView setClipsToBounds:YES]; - - [parallaxView setCustomView:view]; + [view setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight]; + [parallaxView addSubview:view]; parallaxView.scrollView = self; parallaxView.parallaxHeight = height; [self addSubview:parallaxView]; - + parallaxView.originalTopInset = self.contentInset.top; UIEdgeInsets newInset = self.contentInset; @@ -187,27 +185,21 @@ - (id)initWithFrame:(CGRect)frame { - (id)initWithFrame:(CGRect)frame andShadow:(BOOL)shadow { if(self = [super initWithFrame:frame]) { - [self setBackgroundColor:[UIColor clearColor]]; - // default styling values [self setAutoresizingMask:UIViewAutoresizingFlexibleHeight]; [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]; [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]; [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}]]; } } @@ -233,38 +225,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 { @@ -276,7 +243,7 @@ - (void)scrollViewDidScroll:(CGPoint)contentOffset { } if(self.state == APParallaxTrackingActive) { - CGFloat yOffset = contentOffset.y*-1; + CGFloat yOffset = (contentOffset.y*-1) + self.overlap; if ([self.delegate respondsToSelector:@selector(parallaxView:willChangeFrame:)]) { [self.delegate parallaxView:self willChangeFrame:self.frame]; }