【问题标题】:MKMapView pin drag from another viewMKMapView pin 从另一个视图拖动
【发布时间】:2014-07-15 09:13:32
【问题描述】:

我正在考虑在 iOS 上使用 Google Map 街景图钉拖放系统。用户从另一个视图(比如 UINavigationBar)中选择一个图钉并将其拖动到地图上的某个位置,然后放下它。

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

【问题讨论】:

    标签: ios drag-and-drop mkmapview mkannotationview


    【解决方案1】:

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

    1. 制作一个自定义的 UIView,触摸后会跟随触摸移动。

    示例

    -(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. 在地图视图坐标中计算触摸位置并放置图钉。

    希望它为您指明正确的方向。

    【讨论】:

    • 我也是这么想的。没有舒适的方法来实现这一目标。感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 2012-04-06
    • 2011-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多