Swift按顺序绘制多个圆圈

Posted

技术标签:

【中文标题】Swift按顺序绘制多个圆圈【英文标题】:Swift Draw multiple circles in order 【发布时间】:2020-05-14 06:51:39 【问题描述】:

如何在 Swift 中按顺序绘制多个圆圈,当用户在 TextField 中输入数字时,我想在左侧视图中绘制圆圈中的数量:

示例:

到目前为止,这是我的代码:

func drawCircleInsideView(view: UIView, count: Int)
    let halfSize:CGFloat = min(view.bounds.size.width/2, view.bounds.size.height/2) / CGFloat(count)
    let desiredLineWidth:CGFloat = 1
    var i = 0
    var lastPosition = 0
    while i <= count 
      print(“THIAGO: “, i)
      i = i + 1
      let circlePath = UIBezierPath(
          arcCenter: CGPoint(x:halfSize,y:halfSize),
          radius: CGFloat( halfSize - (desiredLineWidth/2) ),
          startAngle: CGFloat(0),
          endAngle:CGFloat(Double.pi * 2),
          clockwise: true)
      let shapeLayer = CAShapeLayer()
      shapeLayer.path = circlePath.cgPath
      shapeLayer.fillColor = UIColor.green.cgColor
      shapeLayer.strokeColor = UIColor.green.cgColor
      shapeLayer.lineWidth = desiredLineWidth
      view.layer.addSublayer(shapeLayer)
      lastPosition = lastPosition + 2
    
  

【问题讨论】:

您尝试过解决方案吗?如果您在这方面遇到任何困难,请告诉我......我也会在附近解决这个问题 【参考方案1】:

你需要使用 i .. 来改变你的圆心

func drawCircleInsideView(view: UIView, count: Int)
      let halfSize:CGFloat = min(view.bounds.size.width/2, view.bounds.size.height/2) / CGFloat(count)
      let desiredLineWidth:CGFloat = 1
      var i = 0
      var lastPosition = 0
      while i <= count 
        print("THIAGO: ", i)
        i = i + 1
        let circlePath = UIBezierPath(
            arcCenter: CGPoint(x:halfSize,y:halfSize + (CGFloat(i) * halfSize*2)),
            radius: CGFloat( halfSize - (desiredLineWidth/2) ),
            startAngle: CGFloat(0),
            endAngle:CGFloat(Double.pi * 2),
            clockwise: true)
        let shapeLayer = CAShapeLayer()
        shapeLayer.path = circlePath.cgPath
        shapeLayer.fillColor = UIColor.green.cgColor
        shapeLayer.strokeColor = UIColor.green.cgColor
        shapeLayer.lineWidth = desiredLineWidth
        view.layer.addSublayer(shapeLayer)
        lastPosition = lastPosition + 2
      
    

结果

【讨论】:

【参考方案2】:

您可以使用视图和约束:

drawCircles(view: UIView, count: Int) 
    for 0...count 
       let circleView = UIView()

        circleView.layer.cornerRadius = 7.5
        circleView.backgroundColor = UIColor.green
        circleView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(circleView)

        circleView.widthAnchor.constraint(equalToConstant: 15).isActive = true
        circleView.heightAnchor.constraint(equalToConstant: 15).isActive = true
        circleView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true

        circleView.topAnchor.constraint(equalTo: view.topAnchor, constant: topMargin).isActive = true

        topMargin = topMargin + 30.0
    

【讨论】:

以上是关于Swift按顺序绘制多个圆圈的主要内容,如果未能解决你的问题,请参考以下文章

通过按JButton在随机JPanel上绘制圆圈

在 UIView (Swift) 中绘制圆形切口

圆圈没有在正确的位置绘制pygame

如何使用 clearRect 不在画布上绘制移动对象

使用Android在xml中使用ShapeDrawable绘制多个形状

iOS:动态更改 UIView