Skip to content

Support for TableView overlap of Parallax View #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions APParallaxHeader/UIScrollView+APParallaxHeader.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down
85 changes: 26 additions & 59 deletions APParallaxHeader/UIScrollView+APParallaxHeader.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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];
Expand All @@ -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;
Expand Down Expand Up @@ -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}]];
}
}

Expand All @@ -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 {
Expand All @@ -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];
}
Expand Down