以编程方式在不同位置创建按钮

时间:2015-12-26 20:02:10

标签: ios swift

我以编程方式创建了UIButtons,并让它们出现在屏幕上的不同位置。我试图防止碰撞但是在测试期间它们仍然会在某些情况下发生。我使用的代码如下,我必须考虑的任何其他逻辑?

class PlayViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    let screenSize: CGRect = UIScreen.mainScreen().bounds


    let buttonWidth = screenSize.width * 0.15;
    let buttonWidth1 = screenSize.width * 0.20;

    var xPos:[Int] = []
    var yPos:[Int] = []

    for (var i = 0; i<4; i++) {


        var xTemp = Int(arc4random_uniform(UInt32(screenSize.width - buttonWidth1)))
        while xPos.contains((xTemp)) {
            xTemp = Int(arc4random_uniform(UInt32(screenSize.width - buttonWidth1)))

        }

        var yTemp = Int(arc4random_uniform(UInt32(screenSize.height - buttonWidth1)))

        while yPos.contains(yTemp) {
            yTemp = Int(arc4random_uniform(UInt32(screenSize.height - buttonWidth1)))
        }


        let xRand = CGFloat(xTemp)

        var yRand = CGFloat(yTemp)
        if yRand <= buttonWidth1 {
            yRand += buttonWidth1
        }

        let but1 = UIButton(frame: CGRect(x: xRand, y: yRand, width: buttonWidth, height: buttonWidth))
        but1.setTitle(String(i), forState: .Normal)
    but1.setTitleColor(UIColor.blueColor(), forState: .Normal)
    but1.addTarget(self, action: "pressed:", forControlEvents: .TouchUpInside)

but1.layer.borderWidth = 2.0
    but1.layer.borderColor = UIColor.blackColor().CGColor

        self.view.addSubview(but1)

        var x = 0
        while (x <= Int(buttonWidth1)) {
            xTemp += 1
            xPos.append(xTemp)
            x++;

        }
        var y = 0
        while (y <= Int(buttonWidth1)) {
            yTemp += 1
            yPos.append(yTemp)
            y++;

        }
    }
    var g = 0

    // Do any additional setup after loading the view.
}

更新代码

 var intersection:[CGRect] = []

    for (var i = 0; i<x; i++) {


        var xTemp = CGFloat(arc4random_uniform(UInt32(screenSize.width - buttonWidth1)))
        var yTemp = CGFloat(arc4random_uniform(UInt32(screenSize.height - buttonWidth1)))

        if yTemp <= buttonWidth1 {
            yTemp += buttonWidth1
        }

        var rect1 = CGRect(x: xTemp, y: yTemp, width: buttonWidth, height: buttonWidth)
        for element in intersection {
                while CGRectIntersectsRect(rect1, element) {
                    xTemp = CGFloat(arc4random_uniform(UInt32(screenSize.width - buttonWidth1)))
                    yTemp = CGFloat(arc4random_uniform(UInt32(screenSize.height - buttonWidth1)))

                    if yTemp <= buttonWidth1 {
                        yTemp += buttonWidth1
                    }

                    rect1 = CGRect(x: xTemp, y: yTemp, width: buttonWidth, height: buttonWidth)
                }

        }

        let but1 = UIButton(frame: rect1)
        but1.setTitle(String(i), forState: .Normal)
        but1.setTitleColor(UIColor.blackColor(), forState: .Normal)
        but1.addTarget(self, action: "pressed:", forControlEvents: .TouchUpInside)
        but1.layer.borderWidth = 2.0
        but1.layer.borderColor = UIColor.blackColor().CGColor

        self.view.addSubview(but1)

        intersection.append(rect1)

    }

2 个答案:

答案 0 :(得分:1)

最好使用CGRectIntersectsRect检查按钮的框架是否重叠。

答案 1 :(得分:0)

第一个rect1未添加到elements数组中。在for

之前创建的rect1