How to slow MKMapCamera movement?

南楼画角 提交于 2019-12-23 05:07:39

问题


I am coding in iOS.

I have an NSArray, which contains a few MKMapCameras. I want to display MKMapCameras from the array one after another.

I put a while loop and used [self.mapView setCamera:nextCamera animated:YES];

However, this is only showing the first and the last views. Everything in between is going too fast.

I want to slow down the movement of each camera. Is there a way to achieve it using CATransaction or using any other animation tricks. If so, could you please show me an example code?

Want to give an update... I tried below code. But it isn't working... Camera movements are fast as I mentioned earlier.

[CATransaction begin];
[CATransaction setAnimationDuration:5.5];
[CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[CATransaction setCompletionBlock:^{
    [self.mapView setCamera:nextCamera animated:YES];
}];
[CATransaction commit];

回答1:


After fiddling with it a few hours, I figured out a way to make it work. Thought of sharing the same with everyone...

I made two changes. I replaced CATransaction with UIView's animation. I also removed Camera's default animation settings, which was conflicting with UIView's animation.

Below is the code.

[UIView beginAnimations:nil context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationDuration:2.5];
self.mapView.camera = nextCamera;
[UIView commitAnimations];



回答2:


According to the WWDC 'Putting MapKit in Perspective' video you should avoid any approach using completion handlers for animating map cameras in sequence. Rather you should set a delegate on your map view and listen for the regionDidChangeAnimated: call to trigger the next camera in your sequence. This way the speed of the camera movement can be controlled with animateWithDuration:

-(void)flyToLocation:(CLLocationCoordinate2D)toLocation {


    CLLocationCoordinate2D startCoord = self.mapView.camera.centerCoordinate;

    CLLocationCoordinate2D eye = CLLocationCoordinate2DMake(toLocation.latitude, toLocation.longitude);


    MKMapCamera *upCam = [MKMapCamera cameraLookingAtCenterCoordinate:startCoord
                                                        fromEyeCoordinate:startCoord
                                                              eyeAltitude:80000];


    MKMapCamera *turnCam = [MKMapCamera cameraLookingAtCenterCoordinate:toLocation
                                                        fromEyeCoordinate:startCoord
                                                              eyeAltitude:80000];

    MKMapCamera *inCam = [MKMapCamera cameraLookingAtCenterCoordinate:toLocation
                                                        fromEyeCoordinate:eye
                                                              eyeAltitude:26000];

    self.camerasArray = [NSMutableArray arrayWithObjects:upCam, turnCam, inCam, nil];

    [self gotoNextCamera];

}

-(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
    [self gotoNextCamera];
}

-(void)gotoNextCamera {

    if (self.camerasArray.count == 0) {
        return;
    }

    MKMapCamera *nextCam = [self.camerasArray firstObject];
    [self.camerasArray removeObjectAtIndex:0];

    [UIView animateWithDuration:3.0 animations:^{
        self.mapView.camera = nextCam;
    }];

}


来源:https://stackoverflow.com/questions/19107550/how-to-slow-mkmapcamera-movement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!