首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在MKCircle上移动MKMapview和拖动MKMapview

在MKCircle上移动MKMapview和拖动MKMapview
EN

Stack Overflow用户
提问于 2014-03-16 18:35:18
回答 2查看 1.5K关注 0票数 2

我有MKCircle on MKMapView。它是用户可拖动的,但是如果用户拖动圆圈外的区域,则地图应该是移动的。

代码语言:javascript
复制
- (IBAction)createPanGestureRecognizer:(id)sender
{
    _mapView.scrollEnabled=NO;
    _panRecognizer = [[UIPanGestureRecognizer alloc]
                      initWithTarget:self action:@selector(respondToPanGesture:)];
    [_mapView addGestureRecognizer:_panRecognizer];
}

-(void)respondToPanGesture:(UIPanGestureRecognizer*)sender {

    static CGPoint originalPoint;

    if (sender.state == UIGestureRecognizerStateBegan) {
        CGPoint point = [sender locationInView:_mapView];
        CLLocationCoordinate2D tapCoordinate = [_mapView convertPoint:point toCoordinateFromView:_mapView];

        CLLocation *tapLocation = [[CLLocation alloc] initWithLatitude:tapCoordinate.latitude longitude:tapCoordinate.longitude];

        CLLocationCoordinate2D originalCoordinate = [_circle coordinate];
        CLLocation *originalLocation = [[CLLocation alloc] initWithLatitude:originalCoordinate.latitude longitude:originalCoordinate.longitude];

        if ([tapLocation distanceFromLocation:originalLocation] > [_circle radius]) {
            _mapView.scrollEnabled=YES;
            _isAllowedToMove=NO;
        }
        else if ([tapLocation distanceFromLocation:originalLocation] < [_circle radius]) {
            originalPoint = [_mapView convertCoordinate:originalCoordinate toPointToView:sender.view];
            _isAllowedToMove=YES;
        }
    }

    if (sender.state == UIGestureRecognizerStateChanged) {
        if (_isAllowedToMove)
        {
            CGPoint translation = [sender translationInView:sender.view];
            CGPoint newPoint    = CGPointMake(originalPoint.x + translation.x, originalPoint.y + translation.y);

            CLLocationCoordinate2D newCoordinate = [_mapView convertPoint:newPoint toCoordinateFromView:sender.view];

            MKCircle *circle2 = [MKCircle circleWithCenterCoordinate:newCoordinate radius:[_circle radius]];
            [_mapView addOverlay:circle2];
            [_mapView removeOverlay:_circle];
            _circle = circle2;
        }
    }

    if (sender.state == UIGestureRecognizerStateEnded || sender.state == UIGestureRecognizerStateFailed || sender.state == UIGestureRecognizerStateCancelled) {
        _mapView.scrollEnabled=NO;
        _isAllowedToMove=NO;
    }
}

拖动圆圈很好,但是当试图拖动地图时,它会保持不变。我的假设是

代码语言:javascript
复制
_mapView.scrollEnabled=YES;

使地图可拖动,但它需要另一个拖动姿态才能启动。如何做到这一点,而不失去移动圈的能力?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-03-17 02:51:20

为了使地图能够在用户开始在圆圈外拖动时被拖动(如果用户开始在圆圈内拖动地图),不要从一开始就禁用scrollEnabled --让它开着(直到拖动开始,我们可以决定是否禁用)。

为了让scrollEnabled开始工作(即。让地图进行自己的摇摄),并添加我们自己的手势识别器,我们需要实现shouldRecognizeSimultaneouslyWithGestureRecognizer并返回YES

更新后的createPanGestureRecognizer:如下所示:

代码语言:javascript
复制
- (IBAction)createPanGestureRecognizer:(id)sender
{
    //_mapView.scrollEnabled=NO;  // <-- do NOT disable scrolling here
    _panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(respondToPanGesture:)];
    _panRecognizer.delegate = self;  // <-- to implement shouldRecognize
    [_mapView addGestureRecognizer:_panRecognizer];
}

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

然后在我们的手势处理程序中,当手势开始时,根据pan的起始位置(在圆圈内外)启用或禁用scrollEnabled

代码语言:javascript
复制
if ([tapLocation distanceFromLocation:originalLocation] > [_circle radius]) {
    _mapView.scrollEnabled=YES;
    _isAllowedToMove=NO;
}
else //if ([tapLocation distanceFromLocation:originalLocation] < [_circle radius]) {
//NOTE: It's not really necessary to check if distance is less than radius
//      in the ELSE part since in the IF we checked if it's greater-than.
//      If we get to the ELSE, we know distance is <= radius.
//      Unless for some reason you want to handle the case where
//      distance exactly equals radius differently but this is unlikely.
{
    originalPoint = [_mapView convertCoordinate:originalCoordinate toPointToView:sender.view];
    _mapView.scrollEnabled=NO;  // <-- disable scrolling HERE
    _isAllowedToMove=YES;
}

最后,在手势结束时始终重新启用scrollEnabled (以防万一)。

当下一个手势开始时,如果需要,它将被重新禁用:

代码语言:javascript
复制
if (sender.state == UIGestureRecognizerStateEnded || sender.state == UIGestureRecognizerStateFailed || sender.state == UIGestureRecognizerStateCancelled) {
    _mapView.scrollEnabled=YES;  // <-- enable instead of disable
    _isAllowedToMove=NO;
}

注意,如果允许用户多次调用createPanGestureRecognizer:,您可能应该将识别器的创建和添加移到其他地方(可能是viewDidLoad),否则多个实例将被添加到映射中。

或者,将按钮更改为切换,以便如果“移动”模式打开,它将从地图中移除手势识别器,而不是创建和添加它。

票数 2
EN

Stack Overflow用户

发布于 2018-08-21 12:59:02

使用快捷4更新答案

包括一些变化:panGesture.maximumNumberOfTouches = 1,以避免检测夹点,缩放为pan手势屏幕点,用于活动区域,可以启动pan手势。

代码语言:javascript
复制
private func addPanGestureRecognizer(to map: MKMapView) {
    let panGesture = UIPanGestureRecognizer(target: self, action: #selector(respondToPanGesture(_:)))
    panGesture.maximumNumberOfTouches = 1
    panGesture.delegate = self
    map.addGestureRecognizer(panGesture)
}

@objc private func respondToPanGesture(_ sender: UIPanGestureRecognizer) {
    guard let map = self.map else {
        return
    }

    let circlePoint = map.convert(startPoint, toPointTo: map) // circle center in View coordinate system

    switch sender.state {
    case .began:
        let point = sender.location(in: map)
        // Set touch radius in points (20), not meters to support different map zoom levels
        if point.radiusContainsPoint(radius: 20, point: circlePoint) {
            // set class property to store initial circle position before start dragging
            initialDragPoint = circlePoint
            map.isScrollEnabled = false
            isDragEnabled = true
        }
        else {
            map.isScrollEnabled = true
            isDragEnabled = false
        }
    case .changed:
        if isDragEnabled {
            let translation = sender.translation(in: map)
            let newPoint = CGPoint(x: initialDragPoint.x + translation.x, y: initialDragPoint.y + translation.y)
            let updatedCoordinate = map.convert(newPoint, toCoordinateFrom: map)
            let newOverlay = makeCircleOverlay(at: updatedCoordinate)
            map.remove(circleOverlay)
            map.add(newOverlay)
            circleOverlay = newOverlay
        }
    case .ended, .failed, .cancelled:
        map.isScrollEnabled = true
        isDragEnabled = false
    case .possible:
        break
    }
}

// MARK: - UIGestureRecognizerDelegate
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    return true
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22441059

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档