Draw circle in random place












2















The question is as follows:
1. It is necessary to realize circles on the screen in a random place. 2. Circles must be of different radii and do not overlap each other and do not go beyond the screen.



Created a separate file for View. Here is the code:



override func draw(_ rect: CGRect) {

var number = 1

while number <= 5 {
//1. Screen center coordinates
let viewMidX = self.bounds.size.height
let viewMidY = self.bounds.size.width

//2. Circle radius
let circleWidth = CGFloat(20 + arc4random_uniform(30))
let circleHeight = circleWidth

//3. Two random number
let randomNumberOne = CGFloat(arc4random_uniform(40))
let randomNumberTwo = CGFloat(arc4random_uniform(20))

//4. Coordinates and sizes of the circle
let rect = CGRect(x: viewMidX - randomNumberOne , y: viewMidY - randomNumberTwo , width: circleWidth, height: circleHeight)

//5. Draw circle
let circlePath = UIBezierPath(roundedRect: rect, cornerRadius: circleWidth / 2)

//6. Circle color
myColor.setFill()
circlePath.fill()


myColor.setStroke()
circlePath.stroke()

number += 1
}


In the ViewController file, I’m doing some animation for this circle (pulsing)



//7.
self.view.transform = CGAffineTransform(scaleX: 1, y: 1)

//8. Animation
UIView.animateKeyframes(withDuration: 1, delay: 0, options: [.autoreverse, .repeat], animations: {
self.view.transform = CGAffineTransform(scaleX: 0.9, y: 0.9)
}) { finished in
}


When I start, I draw 1 circle and nothing more.



enter image description here










share|improve this question




















  • 5





    What is your actual question? Solution for the criteria or why you see only 1 circle?

    – Peter Pajchl
    Nov 23 '18 at 11:29
















2















The question is as follows:
1. It is necessary to realize circles on the screen in a random place. 2. Circles must be of different radii and do not overlap each other and do not go beyond the screen.



Created a separate file for View. Here is the code:



override func draw(_ rect: CGRect) {

var number = 1

while number <= 5 {
//1. Screen center coordinates
let viewMidX = self.bounds.size.height
let viewMidY = self.bounds.size.width

//2. Circle radius
let circleWidth = CGFloat(20 + arc4random_uniform(30))
let circleHeight = circleWidth

//3. Two random number
let randomNumberOne = CGFloat(arc4random_uniform(40))
let randomNumberTwo = CGFloat(arc4random_uniform(20))

//4. Coordinates and sizes of the circle
let rect = CGRect(x: viewMidX - randomNumberOne , y: viewMidY - randomNumberTwo , width: circleWidth, height: circleHeight)

//5. Draw circle
let circlePath = UIBezierPath(roundedRect: rect, cornerRadius: circleWidth / 2)

//6. Circle color
myColor.setFill()
circlePath.fill()


myColor.setStroke()
circlePath.stroke()

number += 1
}


In the ViewController file, I’m doing some animation for this circle (pulsing)



//7.
self.view.transform = CGAffineTransform(scaleX: 1, y: 1)

//8. Animation
UIView.animateKeyframes(withDuration: 1, delay: 0, options: [.autoreverse, .repeat], animations: {
self.view.transform = CGAffineTransform(scaleX: 0.9, y: 0.9)
}) { finished in
}


When I start, I draw 1 circle and nothing more.



enter image description here










share|improve this question




















  • 5





    What is your actual question? Solution for the criteria or why you see only 1 circle?

    – Peter Pajchl
    Nov 23 '18 at 11:29














2












2








2








The question is as follows:
1. It is necessary to realize circles on the screen in a random place. 2. Circles must be of different radii and do not overlap each other and do not go beyond the screen.



Created a separate file for View. Here is the code:



override func draw(_ rect: CGRect) {

var number = 1

while number <= 5 {
//1. Screen center coordinates
let viewMidX = self.bounds.size.height
let viewMidY = self.bounds.size.width

//2. Circle radius
let circleWidth = CGFloat(20 + arc4random_uniform(30))
let circleHeight = circleWidth

//3. Two random number
let randomNumberOne = CGFloat(arc4random_uniform(40))
let randomNumberTwo = CGFloat(arc4random_uniform(20))

//4. Coordinates and sizes of the circle
let rect = CGRect(x: viewMidX - randomNumberOne , y: viewMidY - randomNumberTwo , width: circleWidth, height: circleHeight)

//5. Draw circle
let circlePath = UIBezierPath(roundedRect: rect, cornerRadius: circleWidth / 2)

//6. Circle color
myColor.setFill()
circlePath.fill()


myColor.setStroke()
circlePath.stroke()

number += 1
}


In the ViewController file, I’m doing some animation for this circle (pulsing)



//7.
self.view.transform = CGAffineTransform(scaleX: 1, y: 1)

//8. Animation
UIView.animateKeyframes(withDuration: 1, delay: 0, options: [.autoreverse, .repeat], animations: {
self.view.transform = CGAffineTransform(scaleX: 0.9, y: 0.9)
}) { finished in
}


When I start, I draw 1 circle and nothing more.



enter image description here










share|improve this question
















The question is as follows:
1. It is necessary to realize circles on the screen in a random place. 2. Circles must be of different radii and do not overlap each other and do not go beyond the screen.



Created a separate file for View. Here is the code:



override func draw(_ rect: CGRect) {

var number = 1

while number <= 5 {
//1. Screen center coordinates
let viewMidX = self.bounds.size.height
let viewMidY = self.bounds.size.width

//2. Circle radius
let circleWidth = CGFloat(20 + arc4random_uniform(30))
let circleHeight = circleWidth

//3. Two random number
let randomNumberOne = CGFloat(arc4random_uniform(40))
let randomNumberTwo = CGFloat(arc4random_uniform(20))

//4. Coordinates and sizes of the circle
let rect = CGRect(x: viewMidX - randomNumberOne , y: viewMidY - randomNumberTwo , width: circleWidth, height: circleHeight)

//5. Draw circle
let circlePath = UIBezierPath(roundedRect: rect, cornerRadius: circleWidth / 2)

//6. Circle color
myColor.setFill()
circlePath.fill()


myColor.setStroke()
circlePath.stroke()

number += 1
}


In the ViewController file, I’m doing some animation for this circle (pulsing)



//7.
self.view.transform = CGAffineTransform(scaleX: 1, y: 1)

//8. Animation
UIView.animateKeyframes(withDuration: 1, delay: 0, options: [.autoreverse, .repeat], animations: {
self.view.transform = CGAffineTransform(scaleX: 0.9, y: 0.9)
}) { finished in
}


When I start, I draw 1 circle and nothing more.



enter image description here







ios swift






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 14:01







Рубен Раневский

















asked Nov 23 '18 at 11:19









Рубен РаневскийРубен Раневский

133




133








  • 5





    What is your actual question? Solution for the criteria or why you see only 1 circle?

    – Peter Pajchl
    Nov 23 '18 at 11:29














  • 5





    What is your actual question? Solution for the criteria or why you see only 1 circle?

    – Peter Pajchl
    Nov 23 '18 at 11:29








5




5





What is your actual question? Solution for the criteria or why you see only 1 circle?

– Peter Pajchl
Nov 23 '18 at 11:29





What is your actual question? Solution for the criteria or why you see only 1 circle?

– Peter Pajchl
Nov 23 '18 at 11:29












2 Answers
2






active

oldest

votes


















0














There are a few obvious things wrong right off the bat.



let viewMidX = self.bounds.size.height



should be



let viewMidX = self.bounds.size.width / 2



doing something similar for viewMidY



Next looking at the distance you are setting the random numbers for, the move is minimal. (40 and 20)
A Picture below represents where the circle should be getting drawn at.



enter image description here



So what I would recommend is something more like this.



import UIKit

class ViewController: UIViewController {

let circleView = CircleView()
let tapView = UIView()

override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(circleView)
view.addSubview(tapView)
}

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
circleView.frame = view.frame
tapView.frame = view.frame
}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
animate()
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesEnded(touches, with: event)
circleView.setNeedsDisplay()
}

// Animation
func animate() {
UIView.animateKeyframes(withDuration: 1, delay: 0, options: [.autoreverse, .repeat], animations: {
self.circleView.transform = CGAffineTransform(scaleX: 0.9, y: 0.9)
}, completion: nil)
}

}

class CircleView: UIView {

var myColor = UIColor.red
var borderColor = UIColor.black
var usedRects: [CGRect] =
var usedDiameters: [CGFloat] =

init() {
super.init(frame: .zero)
backgroundColor = .lightGray
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesEnded(touches, with: event)
setNeedsDisplay()
}

override func draw(_ rect: CGRect) {
usedRects =
usedDiameters =
for _ in 0..<5 {

//1. Circle Diameter
let diameter = getDiameter()

//2. Circle Rect
let rect = getRect(diameter: diameter)

//3. Circle Path
let circlePath = UIBezierPath(roundedRect: rect, cornerRadius: diameter / 2)

//4. Circle Color
myColor.setFill()

//5. Draw Circle
circlePath.fill()

//6. Circle Border Color
borderColor.setStroke()

//7. Draw Circle Boder
circlePath.stroke()
}
}

func getDiameter() -> CGFloat {
let diameter = CGFloat(20 + arc4random_uniform(30))
for usedDiameter in usedDiameters
where usedDiameter == diameter {
return getDiameter()
}
usedDiameters.append(diameter)
return diameter
}

func getRect(diameter: CGFloat) -> CGRect {
let randomWidth = CGFloat(arc4random_uniform(UInt32(self.bounds.size.width)))
let randomHeight = CGFloat(arc4random_uniform(UInt32(self.bounds.size.height)))

let rect = CGRect(x: randomWidth, y: randomHeight, width: diameter, height: diameter)

for usedRect in usedRects
where usedRect.intersects(rect) {
return getRect(diameter: diameter)
}
usedRects.append(rect)
return rect
}

}





share|improve this answer































    -1














    You can maybe do a for-cicle to addSubview(circle) in your mainViewController and not in the draw function of your CircleView like this



    ViewController: UIViewController {

    override func viewDidLoad() {
    super.viewDidLoad()
    i = 0
    while i < 5 {
    let circleView = CircleView()
    self.view.addSubView(circleView)
    i += 1
    }
    }
    }


    And in function draw of your custom CircleView do not make a cicle and simply draw the view with random coordinates






    share|improve this answer
























    • Thanks, I will try

      – Рубен Раневский
      Nov 23 '18 at 17:09











    • This is not the right solution.

      – Sethmr
      Nov 23 '18 at 17:44











    • Yeah because i didn't understand the problem :) My solution was for another problem

      – Francesco Destino
      Nov 26 '18 at 8:27












    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53445727%2fdraw-circle-in-random-place%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    There are a few obvious things wrong right off the bat.



    let viewMidX = self.bounds.size.height



    should be



    let viewMidX = self.bounds.size.width / 2



    doing something similar for viewMidY



    Next looking at the distance you are setting the random numbers for, the move is minimal. (40 and 20)
    A Picture below represents where the circle should be getting drawn at.



    enter image description here



    So what I would recommend is something more like this.



    import UIKit

    class ViewController: UIViewController {

    let circleView = CircleView()
    let tapView = UIView()

    override func viewDidLoad() {
    super.viewDidLoad()
    view.addSubview(circleView)
    view.addSubview(tapView)
    }

    override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    circleView.frame = view.frame
    tapView.frame = view.frame
    }

    override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    animate()
    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesEnded(touches, with: event)
    circleView.setNeedsDisplay()
    }

    // Animation
    func animate() {
    UIView.animateKeyframes(withDuration: 1, delay: 0, options: [.autoreverse, .repeat], animations: {
    self.circleView.transform = CGAffineTransform(scaleX: 0.9, y: 0.9)
    }, completion: nil)
    }

    }

    class CircleView: UIView {

    var myColor = UIColor.red
    var borderColor = UIColor.black
    var usedRects: [CGRect] =
    var usedDiameters: [CGFloat] =

    init() {
    super.init(frame: .zero)
    backgroundColor = .lightGray
    }

    required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesEnded(touches, with: event)
    setNeedsDisplay()
    }

    override func draw(_ rect: CGRect) {
    usedRects =
    usedDiameters =
    for _ in 0..<5 {

    //1. Circle Diameter
    let diameter = getDiameter()

    //2. Circle Rect
    let rect = getRect(diameter: diameter)

    //3. Circle Path
    let circlePath = UIBezierPath(roundedRect: rect, cornerRadius: diameter / 2)

    //4. Circle Color
    myColor.setFill()

    //5. Draw Circle
    circlePath.fill()

    //6. Circle Border Color
    borderColor.setStroke()

    //7. Draw Circle Boder
    circlePath.stroke()
    }
    }

    func getDiameter() -> CGFloat {
    let diameter = CGFloat(20 + arc4random_uniform(30))
    for usedDiameter in usedDiameters
    where usedDiameter == diameter {
    return getDiameter()
    }
    usedDiameters.append(diameter)
    return diameter
    }

    func getRect(diameter: CGFloat) -> CGRect {
    let randomWidth = CGFloat(arc4random_uniform(UInt32(self.bounds.size.width)))
    let randomHeight = CGFloat(arc4random_uniform(UInt32(self.bounds.size.height)))

    let rect = CGRect(x: randomWidth, y: randomHeight, width: diameter, height: diameter)

    for usedRect in usedRects
    where usedRect.intersects(rect) {
    return getRect(diameter: diameter)
    }
    usedRects.append(rect)
    return rect
    }

    }





    share|improve this answer




























      0














      There are a few obvious things wrong right off the bat.



      let viewMidX = self.bounds.size.height



      should be



      let viewMidX = self.bounds.size.width / 2



      doing something similar for viewMidY



      Next looking at the distance you are setting the random numbers for, the move is minimal. (40 and 20)
      A Picture below represents where the circle should be getting drawn at.



      enter image description here



      So what I would recommend is something more like this.



      import UIKit

      class ViewController: UIViewController {

      let circleView = CircleView()
      let tapView = UIView()

      override func viewDidLoad() {
      super.viewDidLoad()
      view.addSubview(circleView)
      view.addSubview(tapView)
      }

      override func viewDidLayoutSubviews() {
      super.viewDidLayoutSubviews()
      circleView.frame = view.frame
      tapView.frame = view.frame
      }

      override func viewDidAppear(_ animated: Bool) {
      super.viewDidAppear(animated)
      animate()
      }

      override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
      super.touchesEnded(touches, with: event)
      circleView.setNeedsDisplay()
      }

      // Animation
      func animate() {
      UIView.animateKeyframes(withDuration: 1, delay: 0, options: [.autoreverse, .repeat], animations: {
      self.circleView.transform = CGAffineTransform(scaleX: 0.9, y: 0.9)
      }, completion: nil)
      }

      }

      class CircleView: UIView {

      var myColor = UIColor.red
      var borderColor = UIColor.black
      var usedRects: [CGRect] =
      var usedDiameters: [CGFloat] =

      init() {
      super.init(frame: .zero)
      backgroundColor = .lightGray
      }

      required init?(coder aDecoder: NSCoder) {
      fatalError("init(coder:) has not been implemented")
      }

      override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
      super.touchesEnded(touches, with: event)
      setNeedsDisplay()
      }

      override func draw(_ rect: CGRect) {
      usedRects =
      usedDiameters =
      for _ in 0..<5 {

      //1. Circle Diameter
      let diameter = getDiameter()

      //2. Circle Rect
      let rect = getRect(diameter: diameter)

      //3. Circle Path
      let circlePath = UIBezierPath(roundedRect: rect, cornerRadius: diameter / 2)

      //4. Circle Color
      myColor.setFill()

      //5. Draw Circle
      circlePath.fill()

      //6. Circle Border Color
      borderColor.setStroke()

      //7. Draw Circle Boder
      circlePath.stroke()
      }
      }

      func getDiameter() -> CGFloat {
      let diameter = CGFloat(20 + arc4random_uniform(30))
      for usedDiameter in usedDiameters
      where usedDiameter == diameter {
      return getDiameter()
      }
      usedDiameters.append(diameter)
      return diameter
      }

      func getRect(diameter: CGFloat) -> CGRect {
      let randomWidth = CGFloat(arc4random_uniform(UInt32(self.bounds.size.width)))
      let randomHeight = CGFloat(arc4random_uniform(UInt32(self.bounds.size.height)))

      let rect = CGRect(x: randomWidth, y: randomHeight, width: diameter, height: diameter)

      for usedRect in usedRects
      where usedRect.intersects(rect) {
      return getRect(diameter: diameter)
      }
      usedRects.append(rect)
      return rect
      }

      }





      share|improve this answer


























        0












        0








        0







        There are a few obvious things wrong right off the bat.



        let viewMidX = self.bounds.size.height



        should be



        let viewMidX = self.bounds.size.width / 2



        doing something similar for viewMidY



        Next looking at the distance you are setting the random numbers for, the move is minimal. (40 and 20)
        A Picture below represents where the circle should be getting drawn at.



        enter image description here



        So what I would recommend is something more like this.



        import UIKit

        class ViewController: UIViewController {

        let circleView = CircleView()
        let tapView = UIView()

        override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(circleView)
        view.addSubview(tapView)
        }

        override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        circleView.frame = view.frame
        tapView.frame = view.frame
        }

        override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        animate()
        }

        override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesEnded(touches, with: event)
        circleView.setNeedsDisplay()
        }

        // Animation
        func animate() {
        UIView.animateKeyframes(withDuration: 1, delay: 0, options: [.autoreverse, .repeat], animations: {
        self.circleView.transform = CGAffineTransform(scaleX: 0.9, y: 0.9)
        }, completion: nil)
        }

        }

        class CircleView: UIView {

        var myColor = UIColor.red
        var borderColor = UIColor.black
        var usedRects: [CGRect] =
        var usedDiameters: [CGFloat] =

        init() {
        super.init(frame: .zero)
        backgroundColor = .lightGray
        }

        required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
        }

        override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesEnded(touches, with: event)
        setNeedsDisplay()
        }

        override func draw(_ rect: CGRect) {
        usedRects =
        usedDiameters =
        for _ in 0..<5 {

        //1. Circle Diameter
        let diameter = getDiameter()

        //2. Circle Rect
        let rect = getRect(diameter: diameter)

        //3. Circle Path
        let circlePath = UIBezierPath(roundedRect: rect, cornerRadius: diameter / 2)

        //4. Circle Color
        myColor.setFill()

        //5. Draw Circle
        circlePath.fill()

        //6. Circle Border Color
        borderColor.setStroke()

        //7. Draw Circle Boder
        circlePath.stroke()
        }
        }

        func getDiameter() -> CGFloat {
        let diameter = CGFloat(20 + arc4random_uniform(30))
        for usedDiameter in usedDiameters
        where usedDiameter == diameter {
        return getDiameter()
        }
        usedDiameters.append(diameter)
        return diameter
        }

        func getRect(diameter: CGFloat) -> CGRect {
        let randomWidth = CGFloat(arc4random_uniform(UInt32(self.bounds.size.width)))
        let randomHeight = CGFloat(arc4random_uniform(UInt32(self.bounds.size.height)))

        let rect = CGRect(x: randomWidth, y: randomHeight, width: diameter, height: diameter)

        for usedRect in usedRects
        where usedRect.intersects(rect) {
        return getRect(diameter: diameter)
        }
        usedRects.append(rect)
        return rect
        }

        }





        share|improve this answer













        There are a few obvious things wrong right off the bat.



        let viewMidX = self.bounds.size.height



        should be



        let viewMidX = self.bounds.size.width / 2



        doing something similar for viewMidY



        Next looking at the distance you are setting the random numbers for, the move is minimal. (40 and 20)
        A Picture below represents where the circle should be getting drawn at.



        enter image description here



        So what I would recommend is something more like this.



        import UIKit

        class ViewController: UIViewController {

        let circleView = CircleView()
        let tapView = UIView()

        override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(circleView)
        view.addSubview(tapView)
        }

        override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        circleView.frame = view.frame
        tapView.frame = view.frame
        }

        override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        animate()
        }

        override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesEnded(touches, with: event)
        circleView.setNeedsDisplay()
        }

        // Animation
        func animate() {
        UIView.animateKeyframes(withDuration: 1, delay: 0, options: [.autoreverse, .repeat], animations: {
        self.circleView.transform = CGAffineTransform(scaleX: 0.9, y: 0.9)
        }, completion: nil)
        }

        }

        class CircleView: UIView {

        var myColor = UIColor.red
        var borderColor = UIColor.black
        var usedRects: [CGRect] =
        var usedDiameters: [CGFloat] =

        init() {
        super.init(frame: .zero)
        backgroundColor = .lightGray
        }

        required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
        }

        override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesEnded(touches, with: event)
        setNeedsDisplay()
        }

        override func draw(_ rect: CGRect) {
        usedRects =
        usedDiameters =
        for _ in 0..<5 {

        //1. Circle Diameter
        let diameter = getDiameter()

        //2. Circle Rect
        let rect = getRect(diameter: diameter)

        //3. Circle Path
        let circlePath = UIBezierPath(roundedRect: rect, cornerRadius: diameter / 2)

        //4. Circle Color
        myColor.setFill()

        //5. Draw Circle
        circlePath.fill()

        //6. Circle Border Color
        borderColor.setStroke()

        //7. Draw Circle Boder
        circlePath.stroke()
        }
        }

        func getDiameter() -> CGFloat {
        let diameter = CGFloat(20 + arc4random_uniform(30))
        for usedDiameter in usedDiameters
        where usedDiameter == diameter {
        return getDiameter()
        }
        usedDiameters.append(diameter)
        return diameter
        }

        func getRect(diameter: CGFloat) -> CGRect {
        let randomWidth = CGFloat(arc4random_uniform(UInt32(self.bounds.size.width)))
        let randomHeight = CGFloat(arc4random_uniform(UInt32(self.bounds.size.height)))

        let rect = CGRect(x: randomWidth, y: randomHeight, width: diameter, height: diameter)

        for usedRect in usedRects
        where usedRect.intersects(rect) {
        return getRect(diameter: diameter)
        }
        usedRects.append(rect)
        return rect
        }

        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 17:39









        SethmrSethmr

        1,82311334




        1,82311334

























            -1














            You can maybe do a for-cicle to addSubview(circle) in your mainViewController and not in the draw function of your CircleView like this



            ViewController: UIViewController {

            override func viewDidLoad() {
            super.viewDidLoad()
            i = 0
            while i < 5 {
            let circleView = CircleView()
            self.view.addSubView(circleView)
            i += 1
            }
            }
            }


            And in function draw of your custom CircleView do not make a cicle and simply draw the view with random coordinates






            share|improve this answer
























            • Thanks, I will try

              – Рубен Раневский
              Nov 23 '18 at 17:09











            • This is not the right solution.

              – Sethmr
              Nov 23 '18 at 17:44











            • Yeah because i didn't understand the problem :) My solution was for another problem

              – Francesco Destino
              Nov 26 '18 at 8:27
















            -1














            You can maybe do a for-cicle to addSubview(circle) in your mainViewController and not in the draw function of your CircleView like this



            ViewController: UIViewController {

            override func viewDidLoad() {
            super.viewDidLoad()
            i = 0
            while i < 5 {
            let circleView = CircleView()
            self.view.addSubView(circleView)
            i += 1
            }
            }
            }


            And in function draw of your custom CircleView do not make a cicle and simply draw the view with random coordinates






            share|improve this answer
























            • Thanks, I will try

              – Рубен Раневский
              Nov 23 '18 at 17:09











            • This is not the right solution.

              – Sethmr
              Nov 23 '18 at 17:44











            • Yeah because i didn't understand the problem :) My solution was for another problem

              – Francesco Destino
              Nov 26 '18 at 8:27














            -1












            -1








            -1







            You can maybe do a for-cicle to addSubview(circle) in your mainViewController and not in the draw function of your CircleView like this



            ViewController: UIViewController {

            override func viewDidLoad() {
            super.viewDidLoad()
            i = 0
            while i < 5 {
            let circleView = CircleView()
            self.view.addSubView(circleView)
            i += 1
            }
            }
            }


            And in function draw of your custom CircleView do not make a cicle and simply draw the view with random coordinates






            share|improve this answer













            You can maybe do a for-cicle to addSubview(circle) in your mainViewController and not in the draw function of your CircleView like this



            ViewController: UIViewController {

            override func viewDidLoad() {
            super.viewDidLoad()
            i = 0
            while i < 5 {
            let circleView = CircleView()
            self.view.addSubView(circleView)
            i += 1
            }
            }
            }


            And in function draw of your custom CircleView do not make a cicle and simply draw the view with random coordinates







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 23 '18 at 16:06









            Francesco DestinoFrancesco Destino

            16313




            16313













            • Thanks, I will try

              – Рубен Раневский
              Nov 23 '18 at 17:09











            • This is not the right solution.

              – Sethmr
              Nov 23 '18 at 17:44











            • Yeah because i didn't understand the problem :) My solution was for another problem

              – Francesco Destino
              Nov 26 '18 at 8:27



















            • Thanks, I will try

              – Рубен Раневский
              Nov 23 '18 at 17:09











            • This is not the right solution.

              – Sethmr
              Nov 23 '18 at 17:44











            • Yeah because i didn't understand the problem :) My solution was for another problem

              – Francesco Destino
              Nov 26 '18 at 8:27

















            Thanks, I will try

            – Рубен Раневский
            Nov 23 '18 at 17:09





            Thanks, I will try

            – Рубен Раневский
            Nov 23 '18 at 17:09













            This is not the right solution.

            – Sethmr
            Nov 23 '18 at 17:44





            This is not the right solution.

            – Sethmr
            Nov 23 '18 at 17:44













            Yeah because i didn't understand the problem :) My solution was for another problem

            – Francesco Destino
            Nov 26 '18 at 8:27





            Yeah because i didn't understand the problem :) My solution was for another problem

            – Francesco Destino
            Nov 26 '18 at 8:27


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53445727%2fdraw-circle-in-random-place%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()