简体   繁体   English

将按钮移动到随机位置(快速)

[英]Move button to random position (Swift)

I'm trying to move a button to a random position on the click of another button. 我试图在单击另一个按钮时将一个按钮移动到随机位置。 Here is the code in my viewcontroller. 这是我的视图控制器中的代码。

@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.


}
}

I simply created an outlet for the button I want to move and then an action for the button that moves it. 我只是为要移动的按钮创建了一个插座,然后为要移动它的按钮创建了一个动作。 When I run this, it crashes at launch with the Thread 1 : signal SIGABRT error. 当我运行它时,它在启动时由于线程1崩溃:信号SIGABRT错误。 How do I fix this? 我该如何解决?

So the error you are getting is a NSUnkownKeyExcpetion. 因此,您得到的错误是NSUnkownKeyExcpetion。 What I'm assuming happened is when you wired your view controller in your storyboard to your file something got messed up. 我假设发生的事情是当您将情节提要中的视图控制器连接到文件时,出现了一些混乱。 Right click on your buttons and see if there are two references on any of them. 右键单击您的按钮,查看其中是否有两个引用。 If so delete one. 如果是这样,请删除一个。 You may also be missing an outlet for one of your buttons that it think it should have. 您可能还缺少它认为应该具有的按钮之一的插座。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM