Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Obscure GLKView while app is in background #1254

Merged
merged 3 commits into from
Apr 15, 2015
Merged
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
26 changes: 24 additions & 2 deletions platform/ios/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ @interface MGLMapView () <UIGestureRecognizerDelegate, GLKViewDelegate, CLLocati

@property (nonatomic) EAGLContext *context;
@property (nonatomic) GLKView *glView;
/** A static view to simulate and obscure `glView` when the app is in the background. */
@property (nonatomic) UIView *glSnapshotView;
@property (nonatomic) NSOperationQueue *regionChangeDelegateQueue;
@property (nonatomic) UIImageView *compass;
@property (nonatomic) UIImageView *logoBug;
Expand Down Expand Up @@ -240,6 +242,7 @@ - (BOOL)commonInit
[self insertSubview:_glView atIndex:0];

_glView.contentMode = UIViewContentModeCenter;

[self setBackgroundColor:[UIColor clearColor]];

// load extensions
Expand Down Expand Up @@ -559,8 +562,20 @@ - (void)updateConstraints
// This is the delegate of the GLKView object's display call.
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
mbglView->resize(rect.size.width, rect.size.height, view.contentScaleFactor, view.drawableWidth, view.drawableHeight);
mbglMap->renderSync();
if (self.glSnapshotView)
{
NSUInteger snapshotIdx = [self.subviews indexOfObject:self.glSnapshotView];
NSUInteger glIdx = [self.subviews indexOfObject:self.glView];
NSAssert(snapshotIdx > glIdx,
@"The snapshot view is the %luth subview, behind the GL view, which is the %luth. "
@"The snapshot view should obscure the GL view to prevent crashing while the app is in the background.",
(unsigned long)snapshotIdx, (unsigned long)glIdx);
}
else
{
mbglView->resize(rect.size.width, rect.size.height, view.contentScaleFactor, view.drawableWidth, view.drawableHeight);
mbglMap->renderSync();
}
}

// This gets called when the view dimension changes, e.g. because the device is being rotated.
Expand All @@ -583,6 +598,10 @@ - (void)appDidBackground:(NSNotification *)notification
{
[MGLMapboxEvents flush];

self.glSnapshotView = [self.glView snapshotViewAfterScreenUpdates:YES];
self.glSnapshotView.autoresizingMask = self.glView.autoresizingMask;
[self insertSubview:self.glSnapshotView aboveSubview:self.glView];

mbglMap->stop();

[self.glView deleteDrawable];
Expand All @@ -593,6 +612,9 @@ - (void)appWillForeground:(NSNotification *)notification
[self.glView bindDrawable];

mbglMap->start();

[self.glSnapshotView removeFromSuperview];
self.glSnapshotView = nil;
}

- (void)tintColorDidChange
Expand Down