Created
August 23, 2012 18:40
-
-
Save djibouti33/3440087 to your computer and use it in GitHub Desktop.
Delayed map loading
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)scrollViewDidScroll:(UIScrollView *)scrollView | |
{ | |
// loading the map seems to be the most taxing to the main thread, so | |
// we hold off on loading it until absolutely necessary of if we think | |
// we can do it without the user experiencing any scrolling hitches. | |
// here, we're seeing if the user has scrolled down to the mapShell. | |
// if so, we load the map. | |
// top left of our mapShell | |
float targetPixel = CGRectGetMinY(self.mapShell.frame); | |
// which pixel on the bottom left is currently in view? | |
float lastVisiblePixelOnScreen = scrollView.contentOffset.y + self.frame.size.height; | |
// has the user scrolled below the mapShell? | |
if (!self.mapHasLoaded && lastVisiblePixelOnScreen > targetPixel) { | |
[self loadMap]; | |
} | |
} | |
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate | |
{ | |
// loading the map seems to be the most taxing to the main thread, so | |
// we hold off on loading it until absolutely necessary of if we think | |
// we can do it without the user experiencing any scrolling hitches. | |
// here, we're seeing if the user has scrolled and then lifted their finger, causing no | |
// deceleration. if these conditions are met, it means we can safely load the map, | |
// because the user has either stopped to look/read, or their adjusting their finger | |
// to scroll more. either way, we've got enought time to load the map. | |
if ( !self.mapHasLoaded && !decelerate) { | |
[self loadMap]; | |
} | |
} | |
- (void)loadMap | |
{ | |
[self.map setDelegate:self]; | |
[self.map setCenterCoordinate:self.highlight.coordinate zoomLevel:13 animated:false]; | |
[self.map addAnnotation:self.highlight]; | |
[self.mapShell addSubview:self.map]; | |
self.mapHasLoaded = true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment