Swift - 地图套件在 MapView 之上绘制

问题描述 投票:0回答:0

在我谈论我的问题之前,我会给你一个简短的介绍......

基本上,用户进入这个视图,如果他不想在某个区域上搜索东西,他应该画出该区域。绘制是在 UIImageView 上完成的,它位于地图的顶部。

Final result just for intro

我做的是:

private func drawLine(from fromPoint: CGPoint, to toPoint: CGPoint) {
    // 1
    UIGraphicsBeginImageContext(view.frame.size)
    guard let context = UIGraphicsGetCurrentContext() else {
      return
    }
    drawingImageView.image?.draw(in: view.bounds)
    
    context.move(to: fromPoint)
    context.addLine(to: toPoint)
    
    context.setLineCap(.round)
    context.setBlendMode(.normal)
    context.setLineWidth(5.0)
    context.setStrokeColor(UIColor.orange.cgColor)
    
    context.strokePath()
    
    drawingImageView.image = UIGraphicsGetImageFromCurrentImageContext()
    drawingImageView.alpha = 1.0
    UIGraphicsEndImageContext()
  }
  
  override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    guard let touch = touches.first else {
      return
    }
    
    if(drawClicked != false) {
      swiped = false
      drawingPoints.append(touch.location(in: view))
    } else {
      return
    }
  }
  
  override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    if(drawClicked != false) {
      if !swiped {
        // draw a single point
        if let lastElement = drawingPoints.last {
          drawLine(from: lastElement, to: lastElement)
        }
      }
      
      UIGraphicsBeginImageContext(drawingImageView.frame.size)
      drawingImageView.image?.draw(in: view.bounds, blendMode: .normal, alpha: 1.0)
      drawingImageView.image = UIGraphicsGetImageFromCurrentImageContext()
      UIGraphicsEndImageContext()
      let coordinates = convertPointToCoordinate()
      createPolyline(coordinates: coordinates)
      drawingPoints.removeAll()
      showOrHideLoadingView(true)
      viewModel.getPolygonAds(coordinates: coordinates)
      drawingImageView.image = nil
      drawingImageView.isHidden = true
      drawingImageView.isUserInteractionEnabled = true
      mapView.isZoomEnabled = true
      mapView.isScrollEnabled = true
      mapView.isUserInteractionEnabled = true
      drawClicked = false
    }
  }
  
  override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    guard let touch = touches.first else {
      return
    }
    
    if(drawClicked != false) {
      swiped = true
      let currentPoint = touch.location(in: view)
      if let lastElement = drawingPoints.last {
        drawLine(from: lastElement, to: currentPoint)
      }
      
      drawingPoints.append(currentPoint)
    }
  }

它几乎按预期工作......我完成了所有工作,基本上,用户绘制并将其转换为 MKPolyline 等......最终结果就像一个魅力。

但我的问题是当我画的时候……这只是一些视觉错误,但我不喜欢用户也不会……当我画预览图时,有一个偏移量。但是这种偏移只发生在北方……在南方则不会。另外这只发生在预览中,一旦我 touchEnded,折线就会转到选定的位置。但预览版没有。

我有提示但没有用:

在上面的代码中,我使用了 view.bounds 和 view.size,我知道这是错误的,因为视图父级的大小与 Imageview 不同,我确实将边界更改为“drawingimageview”和“mapview” “用于测试,但从未奏效……它做了一些疯狂的动画……

有人可以给我的任何信息..?

Drawing Preview with the offset...

谢谢!

ios swift mapkit
© www.soinside.com 2019 - 2024. All rights reserved.