簡體   English   中英

如何生成不重疊的隨機按鈕 swift

[英]How to generate random button that does not overlap swift

嗨,我是 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。

例如,您可以制作一個 function 來檢查是否有重疊:

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
    }

然后你可以這樣使用這個 function:

    @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)
    }

筆記:

請注意,因為 while 循環在最壞的情況下可能會永遠循環,所以我建議對其執行次數設置一個限制,例如通過設置maxIteration = 50 ,並在 while 循環中:

    var i = 0 
    while(checkOverlap(...) && i < maxIteration){ 
      //doStuff 
      i += 1
     } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM