Skip to content
This repository has been archived by the owner on Dec 4, 2018. It is now read-only.

Navbar expanding #116

Open
wants to merge 6 commits 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
6 changes: 4 additions & 2 deletions TLYShyNavBar/ShyControllers/TLYShyViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ typedef CGFloat(^TLYShyViewControllerContractionAmountBlock)(UIView *view);
@property (nonatomic, weak) id<TLYShyParent> parent;
@property (nonatomic, weak) TLYShyViewController *subShyController;
@property (nonatomic, weak) UIView *view;

@property (nonatomic, readonly, assign) BOOL isContracted;
@property (nonatomic) TLYShyNavBarFade fadeBehavior;

/* Sticky means it will always stay in expanded state
Expand All @@ -47,7 +47,9 @@ typedef CGFloat(^TLYShyViewControllerContractionAmountBlock)(UIView *view);
- (CGFloat)updateYOffset:(CGFloat)deltaY;

- (CGFloat)snap:(BOOL)contract;
- (CGFloat)snap:(BOOL)contract completion:(void (^)())completion;


- (CGFloat)snap:(BOOL)contract offset:(void (^)(CGFloat deltaY))offset completion:(void (^)())completion;

- (CGFloat)expand;
- (CGFloat)contract;
Expand Down
72 changes: 43 additions & 29 deletions TLYShyNavBar/ShyControllers/TLYShyViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ @implementation TLYShyViewController (AsParent)
- (CGFloat)maxYRelativeToView:(UIView *)superview
{
CGPoint maxEdge = CGPointMake(0, CGRectGetHeight(self.view.bounds));
if ([self.view isKindOfClass:[UINavigationBar class]]){
return maxEdge.y + self.view.frame.origin.y;
}

CGPoint normalizedMaxEdge = [superview convertPoint:maxEdge fromView:self.view];

return normalizedMaxEdge.y;
Expand All @@ -33,16 +37,21 @@ @interface TLYShyViewController ()
@property (nonatomic, assign) CGFloat contractionAmountValue;

@property (nonatomic, assign) CGPoint contractedCenterValue;

@property (nonatomic, assign) BOOL contracted;
@property (nonatomic, assign) BOOL expanded;
@property (nonatomic, readwrite, assign) BOOL isContracted;

@end

@implementation TLYShyViewController

#pragma mark - Properties

- (id) init
{
self = [super init];
if (self){
self.isContracted = YES;
}
return self;
}
// convenience
- (CGPoint)expandedCenterValue
{
Expand Down Expand Up @@ -150,7 +159,7 @@ - (void)offsetCenterBy:(CGPoint)deltaPoint
}

- (CGFloat)updateYOffset:(CGFloat)deltaY
{
{
if (self.subShyController && deltaY < 0)
{
deltaY = [self.subShyController updateYOffset:deltaY];
Expand Down Expand Up @@ -189,10 +198,10 @@ - (CGFloat)updateYOffset:(CGFloat)deltaY

- (CGFloat)snap:(BOOL)contract
{
return [self snap:contract completion:nil];
return [self snap:contract offset:nil completion:nil];
}

- (CGFloat)snap:(BOOL)contract completion:(void (^)())completion
- (CGFloat)snap:(BOOL)contract offset:(void (^)(CGFloat deltaY))offset completion:(void (^)())completion
{
/* "The Facebook" UX dictates that:
*
Expand All @@ -207,22 +216,27 @@ - (CGFloat)snap:(BOOL)contract completion:(void (^)())completion

__block CGFloat deltaY;
[UIView animateWithDuration:0.2 animations:^
{
if ((contract && self.subShyController.contracted) || (!contract && !self.expanded))
{
deltaY = [self contract];
}
else
{
deltaY = [self.subShyController expand];
}
}
{
if ((contract && self.subShyController.contracted) || (!contract && !self.expanded))
{
deltaY = [self contract];
}
else
{
deltaY = [self.subShyController expand];
//Shift scrollView by delta like Facebook does.
if (offset && fabs(deltaY) > FLT_EPSILON) {
offset(deltaY);
}

}
}
completion:^(BOOL finished)
{
if (completion && finished) {
completion();
}
}];
{
if (completion && finished) {
completion();
}
}];

return deltaY;
}
Expand All @@ -234,22 +248,22 @@ - (CGFloat)expand
[self _onAlphaUpdate:1.f];

CGFloat amountToMove = self.expandedCenterValue.y - self.view.center.y;

[self _updateCenter:self.expandedCenterValue];
[self.subShyController expand];

[self _updateCenter:self.expandedCenterValue];
amountToMove += [self.subShyController expand];
self.isContracted = NO;
return amountToMove;
}

- (CGFloat)contract
{
CGFloat amountToMove = self.contractedCenterValue.y - self.view.center.y;

[self _onAlphaUpdate:FLT_EPSILON];

[self _updateCenter:self.contractedCenterValue];
[self.subShyController contract];

[self _updateCenter:self.contractedCenterValue];
amountToMove += [self.subShyController contract];
self.isContracted = YES;
return amountToMove;
}

Expand Down
37 changes: 27 additions & 10 deletions TLYShyNavBar/TLYShyNavBarManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ - (void)setScrollView:(UIScrollView *)scrollView
_scrollView.delegate = (id)self.delegateProxy;
}

[self cleanup];
[self cleanup: YES];
[self layoutViews];

[_scrollView addObserver:self forKeyPath:@"contentSize" options:0 context:kTLYShyNavBarManagerKVOContext];
Expand Down Expand Up @@ -319,8 +319,8 @@ - (void)_handleScrollingEnded
return;
}

__weak __typeof(self) weakSelf;
void (^completion)() = ^
__weak __typeof(self) weakSelf = self;
void (^completion)() = ^()
{
__typeof(self) strongSelf = weakSelf;
if (strongSelf) {
Expand All @@ -336,8 +336,18 @@ - (void)_handleScrollingEnded
}
};


void (^offset)(CGFloat deltaY) = ^(CGFloat deltaY)
{
__typeof(self) strongSelf = weakSelf;
if (strongSelf) {
[strongSelf.scrollView setContentOffset:CGPointMake(0, strongSelf.scrollView.contentOffset.y - deltaY)];
}
};


self.resistanceConsumed = 0;
[self.navBarController snap:self.contracting completion:completion];
[self.navBarController snap:self.contracting offset:offset completion:completion];
}

#pragma mark - KVO
Expand Down Expand Up @@ -389,7 +399,7 @@ - (void)setExtensionView:(UIView *)view

- (void)prepareForDisplay
{
[self cleanup];
[self cleanup: YES];
}

- (void)layoutViews
Expand All @@ -401,9 +411,16 @@ - (void)layoutViews
}
}

- (void)cleanup
- (void)cleanup:(BOOL) expand
{
[self.navBarController expand];

CGFloat deltaY = [self.navBarController expand];
if (expand){
BOOL wasDisabled = self.disable;
self.disable = YES;
[self.scrollView setContentOffset:CGPointMake(0, self.scrollView.contentOffset.y - deltaY)];
self.disable = wasDisabled;
}
self.previousYOffset = NAN;
}

Expand Down Expand Up @@ -469,8 +486,9 @@ + (void)load

- (void)tly_swizzledViewWillAppear:(BOOL)animated
{
[[self _internalShyNavBarManager] prepareForDisplay];
[self tly_swizzledViewWillAppear:animated];
TLYShyNavBarManager * navBarManager = [self _internalShyNavBarManager];
[navBarManager cleanup: navBarManager.navBarController.isContracted];
[self tly_swizzledViewWillAppear:animated];
}

- (void)tly_swizzledViewDidLayoutSubviews
Expand All @@ -481,7 +499,6 @@ - (void)tly_swizzledViewDidLayoutSubviews

- (void)tly_swizzledViewWillDisappear:(BOOL)animated
{
[[self _internalShyNavBarManager] cleanup];
[self tly_swizzledViewWillDisappear:animated];
}

Expand Down
Loading