From 36d90ea97d947dcacc5257bcb33cba139dad46bb Mon Sep 17 00:00:00 2001 From: Adam Hunter Date: Tue, 3 Nov 2015 16:47:40 -0800 Subject: Fix #1975 - improve animation fluidity and response --- platform/ios/MGLMapView.mm | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) (limited to 'platform') diff --git a/platform/ios/MGLMapView.mm b/platform/ios/MGLMapView.mm index 54cc3e879f..347b3a70cb 100644 --- a/platform/ios/MGLMapView.mm +++ b/platform/ios/MGLMapView.mm @@ -40,6 +40,7 @@ #import #import #import +#include class MBGLView; @@ -108,6 +109,9 @@ mbgl::util::UnitBezier MGLUnitBezierForMediaTimingFunction(CAMediaTimingFunction @property (nonatomic, getter=isAnimatingGesture) BOOL animatingGesture; @property (nonatomic, readonly, getter=isRotationAllowed) BOOL rotationAllowed; +@property (nonatomic) CADisplayLink *displayLink; +@property (nonatomic) BOOL needsDisplay; + @end @implementation MGLMapView @@ -124,6 +128,8 @@ mbgl::util::UnitBezier MGLUnitBezierForMediaTimingFunction(CAMediaTimingFunction CLLocationDegrees _pendingLatitude; CLLocationDegrees _pendingLongitude; + + std::mutex needsDisplayMutex; } #pragma mark - Setup & Teardown - @@ -371,6 +377,10 @@ std::chrono::steady_clock::duration secondsAsDuration(float duration) MGLEventKeyZoomLevel: @(zoom), MGLEventKeyPushEnabled: @([MGLMapboxEvents checkPushEnabled]) }]; + self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(updateFromDisplayLink)]; + self.displayLink.frameInterval = 2; + [self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; + _needsDisplay = YES; } - (void)createGLView @@ -386,7 +396,7 @@ std::chrono::steady_clock::duration secondsAsDuration(float duration) // _glView = [[GLKView alloc] initWithFrame:self.bounds context:_context]; _glView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; - _glView.enableSetNeedsDisplay = YES; + _glView.enableSetNeedsDisplay = NO; _glView.drawableStencilFormat = GLKViewDrawableStencilFormat8; _glView.drawableDepthFormat = GLKViewDrawableDepthFormat16; _glView.contentScaleFactor = [UIScreen instancesRespondToSelector:@selector(nativeScale)] ? [[UIScreen mainScreen] nativeScale] : [[UIScreen mainScreen] scale]; @@ -3104,10 +3114,23 @@ CLLocationCoordinate2D MGLLocationCoordinate2DFromLatLng(mbgl::LatLng latLng) return path; } +- (void)updateFromDisplayLink +{ + std::unique_lock lock(needsDisplayMutex); + if (_needsDisplay) { + _needsDisplay = NO; + lock.unlock(); + [self.glView display]; + } else { + lock.unlock(); + } +} + - (void)invalidate { + std::lock_guard lock(needsDisplayMutex); MGLAssertIsMainThread(); - [self.glView setNeedsDisplay]; + _needsDisplay = YES; } - (BOOL)isFullyLoaded -- cgit v1.2.1