簡體   English   中英

將按鈕移動到隨機位置(快速)

[英]Move button to random position (Swift)

我試圖在單擊另一個按鈕時將一個按鈕移動到隨機位置。 這是我的視圖控制器中的代碼。

@IBOutlet weak var samea: UIButton!

@IBAction func yup(sender: UIButton) {

    let buttonWidth = samea.frame.width
    let buttonHeight = samea.frame.height


    let viewWidth = samea.superview!.bounds.width
    let viewHeight = samea.superview!.bounds.height


    let xwidth = viewWidth - buttonWidth
    let yheight = viewHeight - buttonHeight


    let xoffset = CGFloat(arc4random_uniform(UInt32(xwidth)))
    let yoffset = CGFloat(arc4random_uniform(UInt32(yheight)))


    samea.center.x = xoffset + buttonWidth / 2
    samea.center.y = yoffset + buttonHeight / 2

    // Do any additional setup after loading the view, typically from a nib.


}
}

我只是為要移動的按鈕創建了一個插座,然后為要移動它的按鈕創建了一個動作。 當我運行它時,它在啟動時由於線程1崩潰:信號SIGABRT錯誤。 我該如何解決?

因此,您得到的錯誤是NSUnkownKeyExcpetion。 我假設發生的事情是當您將情節提要中的視圖控制器連接到文件時,出現了一些混亂。 右鍵單擊您的按鈕,查看其中是否有兩個引用。 如果是這樣,請刪除一個。 您可能還缺少它認為應該具有的按鈕之一的插座。

暫無
暫無

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

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