我想制作一组圆圈。我创建了一个从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方法仅在整个迭代完成后调用。所以所有圆都具有相同的半径。我怎么解决这个问题?请帮帮我。
更新
圆圈课程中的
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)")
}
答案 0 :(得分:0)
对象应该拥有自己的数据 - 只需将圆圈radius
作为Circle
类本身的属性,这样绘制方法就可以正确绘制圆圈了什么时候打电话。
在更新代码中,只需将圈子管理器更改为以下内容:
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)")
}