【问题标题】:Draw method of the UIView calls after the end of the for loop在 for 循环结束后调用 UIView 的 Draw 方法
【发布时间】:2018-03-02 15:05:56
【问题描述】:

我想制作一组圆圈。我创建了一个从UIView 扩展的圆形类。这是我的绘图方法。

override func draw(_ rect: CGRect) {
    // Drawing code
    self.drawCircle()
}

func drawCircle()
{
    print("centerX-----\(RDcircleEnum.circleCenterX.getValues())")
    print("centerY-------\(RDcircleEnum.circleCenterY.getValues())")
    print("Radius--------\(RDcircleEnum.circleRadius.getValues())")
    let circlePath = UIBezierPath(arcCenter: CGPoint.init(x: RDcircleEnum.circleCenterX.getValues(), y: RDcircleEnum.circleCenterY.getValues()), radius: CGFloat(RDcircleEnum.circleRadius.getValues()), startAngle: CGFloat(0), endAngle:CGFloat(Double.pi * 2), clockwise: true)

    let shapeLayer = CAShapeLayer()
    shapeLayer.path = circlePath.cgPath

    //change the fill color
    shapeLayer.fillColor = UIColor.blue.cgColor
    //you can change the stroke color
    shapeLayer.strokeColor = UIColor.red.cgColor
    //you can change the line width
    shapeLayer.lineWidth = 3.0
    shapeLayer.path = circlePath.cgPath

    self.layer.mask = shapeLayer

}

然后在我的 circleManager 类中我这样做

open func setupCirclestack(parentFrame:CGRect)->[Circle]
{
    var arrayCircles = Array<Any>()
    let arrayColor=[UIColor.yellow,UIColor.blue,UIColor.red]

    for i in 0...2//<CircleValues().numberOfCircles-1
    {
        let circle=self.getInnerCircle(currentFrame: parentFrame) as! Circle
        circle.backgroundColor=arrayColor[i]
        arrayCircles.append(circle)
        let cv=CircleValues.sharedInstance
        cv.radius=cv.radius-20
    }
    return arrayCircles as! [Circle]
}

func getInnerCircle(currentFrame:CGRect)->UIView
{
    print("New Radius------\(CircleValues.sharedInstance.radius)")
    let circle=Circle.init(frame: currentFrame)
    return circle
}

在日志中我看到这样

New Radius------Optional(187.0)
New Radius------Optional(167.0)
New Radius------Optional(147.0)

centerX-----207.0
centerY-------207.0
Radius--------127.0
centerX-----207.0
centerY-------207.0
Radius--------127.0
centerX-----207.0
centerY-------207.0
Radius--------127.0

draw 方法仅在整个迭代完成后调用。所以所有的圆都具有相同的半径。我该如何解决这个问题?请帮帮我。

更新

在 Circle 类中

var innerCircleRadius:CGFloat = 187

在圈子管理器中

for i in 0...2//<CircleValues().numberOfCircles-1
    {

      let circle=self.getInnerCircle(currentFrame: parentFrame) as! Circle
        circle.backgroundColor=arrayColor[i]
        arrayCircles.append(circle)
        circle.innerCircleRadius=circle.innerCircleRadius-20
        print("New Radius------\(circle.innerCircleRadius)")


    }

【问题讨论】:

    标签: ios swift3 uiview draw


    【解决方案1】:

    对象应该保存自己的数据 - 只需将圆的 radius 作为 Circle 类本身的属性,这样无论何时调用 draw 方法都可以正确绘制圆。

    在您的更新代码中,只需将圈子管理器更改为以下内容:

    var currentCircleRadius = CGFloat(180)
    for i in 0...2//<CircleValues().numberOfCircles-1
        {
            let circle=self.getInnerCircle(currentFrame: parentFrame) as! Circle
            circle.backgroundColor=arrayColor[i]
            arrayCircles.append(circle)
            circle.innerCircleRadius = currentCircleRadius
            currentCircleRadius = currentCircleRadius - 20
            print("New Radius------\(circle.innerCircleRadius)")
        }
    

    【讨论】:

    • 我想改变半径并创建不同的圆。
    • 每个圆都有自己的半径..有什么问题?去看看一个面向对象的编程..
    • 没有区别。问题是它在所有迭代之后执行。所以它采用最后分配的半径值。如果我有一个圆圈,它可以工作。但是当我穿过一个循环时,所有的圆都有相同的半径
    • 当然,因为在每个圆中,您访问的变量与半径相同。我告诉过你在 circle 类中创建一个属性,这样每个圆都可以有自己的半径,并且在循环中只需为该属性分配一个适当的值。
    • 你能看到我更新的问题部分吗?我已将内圆半径作为属性。并在每次迭代中尝试将其减少 20。但它仍然相同
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-03
    • 2019-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多