【问题标题】:Drawing lines with core graphics使用核心图形绘制线条
【发布时间】:2017-08-22 11:56:38
【问题描述】:

我想在核心图形中绘制不止一条直线。或保存该行并开始一个新的。我使用了两个变量作为触摸位置,并在 touch Began,Moved,Ended 中为它们分配了触摸位置,然后我使用了这个:

override func draw(_ rect: CGRect) {
    let context = UIGraphicsGetCurrentContext()
    context?.setStrokeColor(UIColor(red: 0, green: 0, blue: 0, alpha: 1.0).cgColor)
    context?.setLineWidth(5.0)
    context?.move(to: CGPoint(x: firstTouchLocation.x, y: firstTouchLocation.y))
    context?.addLine(to: CGPoint(x: lastTouchLocation.x, y: lastTouchLocation.y))
    context?.strokePath()
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    if let touch = touches.first {
        firstTouchLocation = touch.location(in: self)
        lastTouchLocation = firstTouchLocation
        setNeedsDisplay()
    }
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    if let touch = touches.first {
        lastTouchLocation = touch.location(in: self)
        setNeedsDisplay()
    }
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    if let touch = touches.first {
        lastTouchLocation = touch.location(in: self)
        setNeedsDisplay()
    }
}

【问题讨论】:

  • 那有什么问题?
  • 线路消失并开始新的我想保存线路并开始新的
  • @MohmedShowekh 你会在问题中添加描述问题评论吗

标签: ios swift core-graphics


【解决方案1】:

尝试像这样使用 CAShapeLayer 画线:

func addLine(fromPoint start: CGPoint, toPoint end:CGPoint) {
       let line = CAShapeLayer()
       let linePath = UIBezierPath()
       linePath.move(to: start)
       linePath.addLine(to: end)
       line.path = linePath.cgPath
       line.strokeColor = UIColor.red.cgColor
       line.lineWidth = 1
       line.lineJoin = kCALineJoinRound
       self.view.layer.addSublayer(line)
   }

希望这会有所帮助!

【讨论】:

    【解决方案2】:

    你必须有一个模型对象来代表一条线,比如:LineAnnotationLineAnnotation 将保存关于线条的起点和终点、颜色和许多其他详细信息。将每个 LineAnnotation(行)保存在一个数组中。遍历这个数组并一根一根地画线。在您的情况下,正在使用最新数据进行绘图。调用 setNeedsDisplay() 时会刷新先前绘制的点

    【讨论】:

      【解决方案3】:

      根据苹果文档(https://developer.apple.com/documentation/uikit/uiview/1622437-setneedsdisplay) setNeedsDisplay 方法重绘您的视图,因此您只剩下最新行的新鲜视图。

      要解决您的问题,只需将您的代码拉入一个方法并在您进行操作时调用该方法。

      override func draw(_ rect: CGRect) {
          drawLine()
      }
      
      func drawLine()
      {
       let context = UIGraphicsGetCurrentContext()
      context?.setStrokeColor(UIColor(red: 0, green: 0, blue: 0, alpha: 1.0).cgColor)
      context?.setLineWidth(5.0)
      context?.move(to: CGPoint(x: firstTouchLocation.x, y: firstTouchLocation.y))
      context?.addLine(to: CGPoint(x: lastTouchLocation.x, y: lastTouchLocation.y))
      context?.strokePath()
      }
      
      override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
      if let touch = touches.first {
          firstTouchLocation = touch.location(in: self)
          lastTouchLocation = firstTouchLocation
          drawLine()
      }
      }
      
      override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
      if let touch = touches.first {
          lastTouchLocation = touch.location(in: self)
          drawLine()
      }
      }
      
      override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
      if let touch = touches.first {
          lastTouchLocation = touch.location(in: self)
          drawLine()
      }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-23
        • 1970-01-01
        • 1970-01-01
        • 2011-05-22
        • 2016-02-27
        • 1970-01-01
        相关资源
        最近更新 更多