2017-09-21 11 views
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)") 


    } 

答えて

0

オブジェクトは、独自のデータを保持する必要があります - ちょうどCircleクラス自体のプロパティとして円のradiusを置きます描画メソッドは、oに関係なく円を適切に描くことができますnが呼び出されたとき。あなたの更新コードで

、ちょうど次に円マネージャを変更します。

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)") 
    } 
+0

私は半径を変更し、別の円を作成したいです。 – das

+0

各円は独自の半径を持ちます..問題は何ですか?行って、オブジェクト指向プログラミングを見てください。 –

+0

違いはありません。問題はそれがすべての反復の後に実行されることです。したがって、最後に割り当てられた半径値が使用されます。私が1つのサークルを持っていれば、それは機能します。しかし、私はループを実行すると、すべての円は同じ半径を持っています – das

関連する問題