【发布时间】:2021-01-16 13:59:56
【问题描述】:
我在下面有一个循环,它应该检测UIView 数组中的视图是否完全相互重叠。如果是,则调整其center 值。
每个视图都是 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
}
}
}
}
...
}
以下是生成随机CGPoint 的方法,该CGPoint 设置为视图的中心:
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
}
下面是确定两个UIView之间距离的方法。
func distance(_ a: CGPoint, _ b: CGPoint) -> CGFloat {
let xDist = a.x - b.x
let yDist = a.y - b.y
return CGFloat(hypot(xDist, yDist))
}
但是,我仍然偶尔会遇到两个视图相互重叠的情况(请参阅下面的红圈部分):
编辑这是将圆圈添加到视图的代码:
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()
}
【问题讨论】:
-
我预计您仍然会出现重叠,因为您只是将 两个 视图分开。如果没有我尝试运行您的代码,如果您的随机定位以
v1、v2和v3结尾会发生什么情况:i.stack.imgur.com/fb1k0.png?您的代码是否要处理将v3从v1移开,只是让它与v2重叠?然后,可能,将v3从v2移开......再次将其重新与v1重叠? -
@DonMag 我认为这是有道理的/描述了正在发生的事情
-
为什么不改变你的 getRandomPoint() 的逻辑,让它只返回一个有效的随机点,然后你就不用担心移动圆圈了。
-
@valosip 好吧,理论上屏幕上的任何点都是有效的;这只是确定重叠的问题(或者我可能没有关注你?)
-
@narner 您在视图中添加圈子的方式和位置?