【发布时间】: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)")
}
【问题讨论】: