Keep two MKMapViews showing the same region

前端 未结 3 894
情深已故
情深已故 2021-01-20 14:30

On my iPad app, I have 2 mapViews that are the same size displayed next to each other. I want these to always show the same area. I achieve this now using the regionDidCha

3条回答
  •  醉话见心
    2021-01-20 14:57

    You could use UIGestureRecognizer to help keep the maps more in sync as the user is manipulating one of the maps.

    For example, using the UIPanGestureRecognizer, the gesture recognizer action handler will fire multiple times while the user is panning the map--unlike regionDidChangeAnimated which fires only when the pan is finished.

    You'll need to add a gesture recognizer to one or both maps and implement your custom gesture handler method. Also implement the shouldRecognizeSimultaneouslyWithGestureRecognizer delegate method and return YES so your gesture handler can work together with the map's.

    Example:

    //add the gesture handler to map(s)...
    UIPanGestureRecognizer *pgr = [[UIPanGestureRecognizer alloc] 
        initWithTarget:self action:@selector(gestureHandler:)];
    pgr.delegate = self;
    [mapViewA addGestureRecognizer:pgr];
    [pgr release];
    
    //...
    
    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer 
        shouldRecognizeSimultaneouslyWithGestureRecognizer:
            (UIGestureRecognizer *)otherGestureRecognizer
    {
        return YES;
    }
    
    - (void)gestureHandler:(UIGestureRecognizer *)gestureRecognizer
    {
        [mapViewB setRegion:mapViewA.region animated:NO];
    }
    

    If you want to add the gesture recognizer to both, you'll need to create a separate instance for each map (ie. you can't add pgr to both maps). You might also want/need to add UIPinchGestureRecognizer and UITapGestureRecognizer. You can use the same handler method for all the recognizers though.

    I'd still implement regionDidChangeAnimated just in case a gesture is missed.

提交回复
热议问题