Swift 如何生成不重叠的随机按钮

Swift 如何生成不重叠的随机按钮,swift,Swift,嗨,我是Swift语言的新手,所以我尝试创建一个泡泡流行游戏,所以我想创建的泡泡基本上是一个UI按钮,所以我会随机生成泡泡,我想做的是生成一个彼此不重叠的泡泡 这是我的密码 @objc func generateBubble() { let bubble = Bubble() bubble.animation() let xPosition = Int.random(in: Int(viewBubble.frame.minX) + 30 ... I

嗨,我是Swift语言的新手,所以我尝试创建一个泡泡流行游戏,所以我想创建的泡泡基本上是一个UI按钮,所以我会随机生成泡泡,我想做的是生成一个彼此不重叠的泡泡

这是我的密码

@objc func generateBubble() {
        let bubble = Bubble()
        bubble.animation()
        let xPosition = Int.random(in: Int(viewBubble.frame.minX) + 30 ... Int(viewBubble.frame.maxX) - 30)
        let yPosition = Int.random(in: Int(viewBubble.frame.minY) + 50 ... Int(viewBubble.frame.maxY) - 50)
        bubble.addTarget(self, action: #selector(bubblePressed), for: .touchUpInside)
        bubble.frame = CGRect(x: xPosition, y: yPosition, width: 50, height: 50)
        bubble.layer.cornerRadius = 0.5 * bubble.bounds.size.width
        self.view.addSubview(bubble)
    }
    

    
    @IBAction func bubblePressed(_ sender: UIButton) {
        if Bubble().backgroundColor == UIColor.red{
            self.score += 1;
            
        }
        else if Bubble().backgroundColor == UIColor.green{
            self.score += 5;
            
        }
        if Bubble().backgroundColor == UIColor.black{
            self.score += 10;
            
        }
        if Bubble().backgroundColor == UIColor.blue{
            self.score += 8;
            
        }
        
        sender.removeFromSuperview()
    }

有什么建议吗?

实现此结果的一种方法是在屏幕上循环所有气泡,并根据此结果调整新气泡的X和Y

例如,您可以创建一个函数来检查是否存在重叠:

func checkOverlap(x: CGFloat, y: CGFloat, view: UIView, offset: CGFloat) -> Bool{
        for case let bubble as UIButton in view.subviews {
            let current_x = bubble.frame.origin.x
            let current_y = bubble.frame.origin.y

            if((x < current_x + offset && x > current_x) && (y < current_y + offset && y > current_y)){
                  //OVERLAP
                  return true
            }
        }
        // NO OVERLAP
        return false
    }

注: 请注意,因为while循环在最坏的情况下可能会永远循环,所以我建议对其执行次数设置一个限制,例如通过设置
maxIteration=50
,在while循环中:

var i=0
而(checkOverlap(…)&&i
hi感谢您的回复我刚刚实现了您的代码气泡仍然重叠hi,我测试了代码,UIButtons的生成没有重叠。它们在产卵期间重叠?或者它们在动画中移动和重叠?在第一种情况下,可能必须更改x和y更改逻辑。在第二种情况下,您应该实现一个函数来检查两个气泡是否碰撞。它可以很容易地实现使用精灵,但与UIButtons你需要实现它自己
    @objc func generateBubble() {
        let bubble = Bubble()
        bubble.animation()
        // I suggest to use CGFloat.random
        let xPosition = CGFloat.random(in: viewBubble.frame.minX + 30 ... viewBubble.frame.maxX - 30)
        let yPosition = CGFloat.random(in: viewBubble.frame.minY + 50 ... viewBubble.frame.maxY - 50)
        
        // Offset is bubbleWidth/2      
        while(checkOverlap(x: xPosition, y: yPosition, view: self.view, offset: 25)){
        // Here you can calculate x and y randomly again or you can
        // add offset if you want, or any other calculation you want to move the 
        // new bubble
        // Example 1, just get new random x and y:
          xPosition = CGFloat.random(in: viewBubble.frame.minX + 30 ... viewBubble.frame.maxX - 30)
          yPosition = CGFloat.random(in: viewBubble.frame.minY + 50 ... viewBubble.frame.maxY - 50)

        // Example 2, move x and/or y by offset, or other number with - or +:
           xPosition += 25 // or xPosition -= 25, or you can choose randomly
           yPosition += 25 // or yPosition -= 25, or you can choose randomly

        // Example 3, random guess in any combination you want:
         if(CGFloat.random(in: 0 ... 10) > 5){
            xPosition += 25
            yPosition -= 25
         }else{
            xPosition -= 25
            yPosition += 25
          }
        }

        bubble.addTarget(self, action: #selector(bubblePressed), for: .touchUpInside)
        bubble.frame = CGRect(x: xPosition, y: yPosition, width: 50, height: 50)
        bubble.layer.cornerRadius = 0.5 * bubble.bounds.size.width
        self.view.addSubview(bubble)
    }
    var i = 0 
    while(checkOverlap(...) && i < maxIteration){ 
      //doStuff 
      i += 1
     }