繁体   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