MKMapView从另一个视图引脚拖动



我在考虑iOS上的谷歌地图街景拖放系统。用户从另一个视图(假设是UINavigationBar)中选择一个pin,并将其拖到地图上的某个位置,然后将其放下。

我有点迷路了。对于这种类型的交互,您有一个工作流程吗?

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

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

希望它能指引你正确的方向。

最新更新