0
円のセットを作成したいです。 UIView
から拡張されたサークルクラスを作成しました。これが私のドロー方法です。forループの終了後の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メソッドは、全体の反復が終了した後にのみ呼び出されます。だから、すべての円は同じ半径を持っています。これをどうすれば解決できますか?私を助けてください。
UPDATE Circleクラスで
circleManagerで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)")
}
私は半径を変更し、別の円を作成したいです。 – das
各円は独自の半径を持ちます..問題は何ですか?行って、オブジェクト指向プログラミングを見てください。 –
違いはありません。問題はそれがすべての反復の後に実行されることです。したがって、最後に割り当てられた半径値が使用されます。私が1つのサークルを持っていれば、それは機能します。しかし、私はループを実行すると、すべての円は同じ半径を持っています – das