diff --git a/ios/MBXViewController.mm b/ios/MBXViewController.mm index d700fb9b2..2034f5ff7 100644 --- a/ios/MBXViewController.mm +++ b/ios/MBXViewController.mm @@ -16,6 +16,8 @@ #include #include +NSString *const MBXNeedsRenderNotification = @"MBXNeedsRenderNotification"; + @interface MBXViewController () @property (nonatomic) EAGLContext *context; @@ -77,9 +79,6 @@ class MBXMapView [EAGLContext setCurrentContext:self.context]; - displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(render:)]; - [displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode]; - mapView->init(); UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)]; @@ -112,6 +111,25 @@ class MBXMapView UILongPressGestureRecognizer *threeFingerLongPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleThreeFingerLongPressGesture:)]; threeFingerLongPress.numberOfTouchesRequired = 3; [self.view addGestureRecognizer:threeFingerLongPress]; + + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(startRender) name:MBXNeedsRenderNotification object:nil]; + + displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(render:)]; + [displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode]; +} + +- (void)startRender +{ + [NSObject cancelPreviousPerformRequestsWithTarget:self]; + + displayLink.paused = NO; + + [self performSelector:@selector(stopRender) withObject:nil afterDelay:3.0]; +} + +- (void)stopRender +{ + displayLink.paused = YES; } - (void)render:(id)sender @@ -162,6 +180,8 @@ class MBXMapView mapView->map.moveBy(finalCenter.x - self.center.x, finalCenter.y - self.center.y, duration); } + + [self startRender]; } - (void)handlePinchGesture:(UIPinchGestureRecognizer *)pinch @@ -188,6 +208,8 @@ class MBXMapView mapView->map.scaleBy(powf(2, newZoom) / mapView->map.getScale(), [pinch locationInView:pinch.view].x, [pinch locationInView:pinch.view].y); } + + [self startRender]; } - (void)handleRotateGesture:(UIRotationGestureRecognizer *)rotate @@ -200,30 +222,40 @@ class MBXMapView { mapView->map.setAngle(self.angle + rotate.rotation, [rotate locationInView:rotate.view].x, [rotate locationInView:rotate.view].y); } + + [self startRender]; } - (void)handleDoubleTapGesture:(UITapGestureRecognizer *)doubleTap { if (doubleTap.state == UIGestureRecognizerStateEnded) mapView->map.scaleBy(2, [doubleTap locationInView:doubleTap.view].x, [doubleTap locationInView:doubleTap.view].y, 0.5); + + [self startRender]; } - (void)handleTwoFingerTapGesture:(UITapGestureRecognizer *)twoFingerTap { if (twoFingerTap.state == UIGestureRecognizerStateEnded) mapView->map.scaleBy(0.5, [twoFingerTap locationInView:twoFingerTap.view].x, [twoFingerTap locationInView:twoFingerTap.view].y, 0.5); + + [self startRender]; } - (void)handleLongPressGesture:(UILongPressGestureRecognizer *)longPress { if (longPress.state == UIGestureRecognizerStateBegan) mapView->map.resetNorth(); + + [self startRender]; } - (void)handleTwoFingerLongPressGesture:(UILongPressGestureRecognizer *)twoFingerLongPress { if (twoFingerLongPress.state == UIGestureRecognizerStateBegan) mapView->map.resetPosition(); + + [self startRender]; } - (void)handleThreeFingerLongPressGesture:(UILongPressGestureRecognizer *)threeFingerLongPress @@ -234,6 +266,8 @@ class MBXMapView self.debug = ! self.debug; } + + [self startRender]; } - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer @@ -291,7 +325,9 @@ namespace llmr func(res); } - }]; + + [[NSNotificationCenter defaultCenter] postNotificationName:MBXNeedsRenderNotification object:nil]; + }]; } void request_http(std::string url, std::function func, std::function cb) @@ -302,18 +338,20 @@ namespace llmr queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { - Response res; + Response res; - if (error == nil) - { - res.code = [(NSHTTPURLResponse *)response statusCode]; - res.body = { (const char *)[data bytes], [data length] }; - } + if (error == nil) + { + res.code = [(NSHTTPURLResponse *)response statusCode]; + res.body = { (const char *)[data bytes], [data length] }; + } - func(res); + func(res); - cb(); - }]; + cb(); + + [[NSNotificationCenter defaultCenter] postNotificationName:MBXNeedsRenderNotification object:nil]; + }]; } double time()