Skip to content

Commit

Permalink
Springy map header
Browse files Browse the repository at this point in the history
  • Loading branch information
davidchiles committed Aug 4, 2014
1 parent b636714 commit f008dce
Showing 1 changed file with 59 additions and 7 deletions.
66 changes: 59 additions & 7 deletions iBurn/BRCDetailViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,21 @@
#import "RMMarker+iBurn.h"
#import "BRCDetailInfoTableViewCell.h"
#import "BRCDetailMapViewController.h"
#import "PureLayout.h"

static NSString * const kBRCRowHeightDummyCellIdentifier = @"kBRCRowHeightDummyCellIdentifier";

static CGFloat const kMapHeaderOffsetY = -150.0;
static CGFloat const kMapHeaderHeight = 250.0;

@interface BRCDetailViewController () <UITableViewDataSource, UITableViewDelegate, MFMailComposeViewControllerDelegate, RMMapViewDelegate>

@property (nonatomic, strong) BRCDataObject *dataObject;
@property (nonatomic, strong) NSArray *detailCellInfoArray;
@property (nonatomic, strong) UIBarButtonItem *favoriteBarButtonItem;
@property (nonatomic, strong) RMMapView *mapView;
@property (nonatomic) BOOL addedContraints;
@property (nonatomic, strong) UIView *fakeTableViewBackground;
@property (nonatomic) BOOL didSetContraints;

@end

Expand Down Expand Up @@ -65,21 +70,33 @@ - (void)viewDidLoad
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.view.backgroundColor = self.tableView.backgroundColor;
self.tableView.backgroundView = nil;
self.tableView.backgroundView = [[UIView alloc] init];
self.tableView.backgroundColor = [UIColor clearColor];

[self.tableView registerClass:[BRCDetailInfoTableViewCell class] forCellReuseIdentifier:[BRCDetailInfoTableViewCell cellIdentifier]];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kBRCRowHeightDummyCellIdentifier];

[self.view addSubview:self.tableView];

if (self.mapView) {
UIView *mapContainerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 150, 150)];
self.mapView.frame = mapContainerView.frame;
[mapContainerView addSubview:self.mapView];
CGRect mapFrame = CGRectMake(0.0, 0.0, self.view.bounds.size.width, kMapHeaderHeight);
self.fakeTableViewBackground = [[UIView alloc] init];
self.fakeTableViewBackground.backgroundColor = self.view.backgroundColor;
self.fakeTableViewBackground.translatesAutoresizingMaskIntoConstraints = NO;

[mapContainerView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapMapContainerview:)]];
self.mapView.frame = CGRectMake(0, kMapHeaderOffsetY, self.view.bounds.size.width, self.view.bounds.size.height + ABS(kMapHeaderOffsetY));
UIView *tableHeader = [[UIView alloc] initWithFrame: mapFrame];
[tableHeader addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapMapContainerview:)]];
tableHeader.backgroundColor = [UIColor clearColor];
self.tableView.tableHeaderView = tableHeader;

self.tableView.tableHeaderView = mapContainerView;
[self.view insertSubview:self.mapView belowSubview:self.tableView];
[self.view insertSubview:self.fakeTableViewBackground belowSubview:self.tableView];
}

[self.view addSubview:self.tableView];


self.favoriteBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[self currentStarImage] style:UIBarButtonItemStylePlain target:self action:@selector(didTapFavorite:)];

Expand All @@ -92,6 +109,20 @@ - (void)viewDidAppear:(BOOL)animated
[self.mapView brc_zoomToIncludeCoordinate:self.dataObject.location.coordinate andCoordinate:self.mapView.userLocation.location.coordinate animated:animated];
}

- (void)updateViewConstraints
{
[super updateViewConstraints];
if (!self.didSetContraints) {

[self.fakeTableViewBackground autoAlignAxisToSuperviewAxis:ALAxisVertical];
[self.fakeTableViewBackground autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(0, 0, 0, 0) excludingEdge:ALEdgeTop];
[self.fakeTableViewBackground autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.tableView.tableHeaderView];


self.didSetContraints = YES;
}
}

- (UIImage*)imageIfFavorite:(BOOL)isFavorite {
UIImage *starImage = nil;
if (isFavorite) {
Expand Down Expand Up @@ -275,6 +306,27 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath

}

#pragma - mark UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat scrollOffset = scrollView.contentOffset.y;
CGRect headerFrame = self.mapView.frame;

if (scrollOffset < 0)
{
headerFrame.origin.y = MIN(kMapHeaderOffsetY - ((scrollOffset / 3)), 0);

}
else //scrolling up
{
headerFrame.origin.y = kMapHeaderOffsetY - scrollOffset;
}

self.mapView.frame = headerFrame;
}


#pragma - mark MFMailComposeViewControllerDelegate

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
Expand Down

0 comments on commit f008dce

Please sign in to comment.