I have a loop below that is supposed to detect if a view in an array of UIView
's overlap with each other at all. If they do, then adjust their center
value.
Each view is 25x25.
func drawCircles() {
for i in 0..<circles.count{
circles[i].center = getRandomPoint()
for j in 0..<circles.count{
if(i != j) {
let comparingCentre = circles[j].center
let dist = distance(comparingCentre, circles[i].center)
if dist <= 25 {
var newCenter = circles[i].center
var centersVector = CGVector(dx: newCenter.x - comparingCentre.x, dy: newCenter.y - comparingCentre.y)
//Circle width is 25
centersVector.dx *= 26 / dist
centersVector.dy *= 26 / dist
newCenter.x = comparingCentre.x + centersVector.dx
newCenter.y = comparingCentre.y + centersVector.dy
circles[i].center = newCenter
}
}
}
}
...
}
Below is the method for generating a random CGPoint
that gets set to the view's center:
func getRandomPoint() -> CGPoint {
let viewMidX = self.circlesView.bounds.midX
let viewMidY = self.circlesView.bounds.midY
let xPosition = self.circlesView.frame.midX - viewMidX + CGFloat(arc4random_uniform(UInt32(viewMidX*2)))
let yPosition = self.circlesView.frame.midY - viewMidY + CGFloat(arc4random_uniform(UInt32(viewMidY*2)))
let point = CGPoint(x: xPosition, y: yPosition)
return point
}
And below is the method for determining the distance between two UIView
's.
func distance(_ a: CGPoint, _ b: CGPoint) -> CGFloat {
let xDist = a.x - b.x
let yDist = a.y - b.y
return CGFloat(hypot(xDist, yDist))
}
However, I'm still experiencing, occasionally, when two view's will overlap each other (see the red-circled section below):
EDIT This is code that adds the circles to the view:
func generateCircles() {
numberOfCircles = Int.random(in: 1..<50)
let circleWidth = CGFloat(25)
let circleHeight = circleWidth
var i = 0
while i < numberOfCircles {
let circleView = CircleView(frame: CGRect(x: 0.0, y: 0.0, width: circleWidth, height: circleHeight))
let number = Int.random(in: 0..<2)
if number == 1 {
circleView.mainColor = .yellow
} else {
circleView.mainColor = .blue
}
circles.append(circleView)
i += 1
}
drawCircles()
}