MKMapView引脚从另一个视图拖动

时间:2014-07-15 09:13:33

标签: ios drag-and-drop mkmapview mkannotationview

我正在思考iOS上的Google Map街景视图引脚拖放系统。用户从另一个视图中选择一个引脚(让我们说UINavigationBar)并将其拖动到地图上的某个位置然后放下它。

我有点迷失了。您是否有此类互动的工作流程?

1 个答案:

答案 0 :(得分:1)

这是一项相当复杂的任务,但可以分为几个简单的步骤。

  1. 制作一个自定义UIView,触摸后会跟随触摸动作。
  2. 实施例

    -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        _originalPosition = self.view.center;
        _touchOffset = CGPointMake(self.view.center.x-position.x,self.view.center.y-position.y);
    }
    
    -(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    {        
        UITouch *touch = [touches anyObject];
        CGPoint position = [touch locationInView: self.view.superview];
    
        [UIView animateWithDuration:.001
                              delay:0.0
                            options:UIViewAnimationOptionCurveEaseInOut
                         animations:^ {
    
                             self.view.center = CGPointMake(position.x+_touchOffset.x, position.y+_touchOffset.y);
                         }
                         completion:^(BOOL finished) {}];
    }
    
    -(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    {
        CGPoint positionInView = [touch locationInView:self.view];
        CGPoint newPosition;
        if (CGRectContainsPoint(_desiredView.frame, positionInView)) {
            newPosition = positionInView;
            // _desiredView is view where the user can drag the view
        } else {
            newPosition = _originalPosition;
            // its outside the desired view so lets move the view back to start position
        }
    
        [UIView animateWithDuration:0.4
                              delay:0.0
                            options:UIViewAnimationOptionCurveEaseInOut
                         animations:^ {
                             self.view.center = newPosition
                             // to 
                         }
                         completion:^(BOOL finished) {}];
    
    }
    
    1. 当用户松开手指时,您必须获取触摸位置。
    2. 计算地图视图坐标中的触摸位置并放置图钉。
    3. 希望它指出你正确的方向。