0
A
回答
4
使用此爲您的按鈕的@IBAction
:
@IBAction func moveButton(button: UIButton) {
// Find the button's width and height
let buttonWidth = button.frame.width
let buttonHeight = button.frame.height
// Find the width and height of the enclosing view
let viewWidth = button.superview!.bounds.width
let viewHeight = button.superview!.bounds.height
// Compute width and height of the area to contain the button's center
let xwidth = viewWidth - buttonWidth
let yheight = viewHeight - buttonHeight
// Generate a random x and y offset
let xoffset = CGFloat(arc4random_uniform(UInt32(xwidth)))
let yoffset = CGFloat(arc4random_uniform(UInt32(yheight)))
// Offset the button's center by the random offsets.
button.center.x = xoffset + buttonWidth/2
button.center.y = yoffset + buttonHeight/2
}
警告:如果您有自動版式啓用,您可以按鍵彈回原來的位置當子視圖被佈置時。請參閱此問題的解決方案here。
相關問題
- 1. 將按鈕移動到隨機位置(Swift)
- 2. (Swift)將按鈕移動到隨機位置有例外
- 3. 如何將按鈕移動到隨機位置?
- 4. 按鈕不隨機移動
- 5. 將BxSlider按鈕移動到位置
- 6. 如何讓單位隨機移動單選按鈕?
- 7. 如何將按鈕移動到精確位置?
- 8. 如何將對象移動到某個隨機位置,然後在到達新位置時旋轉並移動到新的隨機位置?
- 9. 基於隨機數的按鈕位置 - xCode 6(swift)
- 10. 隨機移動/湍流 - SWIFT
- 11. 提交按鈕的隨機位置
- 12. 隨機按鈕位置Reeaction遊戲
- 13. 試圖將龜對象移動到隨機位置
- 14. 將框移動到隨機位置與angularjs
- 15. Android:將按鈕設置爲隨機位置
- 16. 將按鈕從活動外部移動到特定位置
- 17. 按下按鈕,將身體從一個位置移動到另一個位置
- 18. 如何將按鈕移動到中心?
- 19. 按鈕在移動後重置位置
- 20. 如何自動將按鈕移動到另一個按鈕?
- 21. Android如何隨機調換按鈕位置?
- 22. 如何在隨機屏幕位置顯示按鈕
- 23. 移動圖像到隨機佔位符
- 24. 隨機放置按鈕
- 25. iOS快速將我的位置按鈕移動到右上角
- 26. 如何拖動屏幕上的按鈕並將翻譯動畫從當前位置移動到原始位置?
- 27. 如何將按鈕移動到第一個文本列的中間位置?
- 28. 將對象從隨機位置移動一圈
- 29. 如何將「onClick」從一個按鈕移動到wifi設置
- 30. Android:我如何在活動中隨機放置一組按鈕?
這樣做。謝謝。 – perte 2014-09-27 14:36:47