【问题标题】:Swift Draw multiple circles in orderSwift按顺序绘制多个圆圈
【发布时间】: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
    }
  }

【问题讨论】:

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

标签: ios swift uiview drawing uibezierpath


【解决方案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
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-03
      相关资源
      最近更新 更多