我正在创建按钮并将它们显示为一个圆圈。我希望用户选择一些。我需要做6次。我想弄清楚要显示新集合时如何摆脱旧按钮。
我尝试创建按钮数组并将其宽度和高度设置为零,但是当我尝试将它们放置在数组中时,它们将不会显示。
func createButtons(wheelNumber: Int) {
for i in 0..<wheels[wheelNumber].length {
let button = MyButton()
let radius = CGFloat(200.0)
button.backgroundColor = UIColor.green
button.setTitle(wheels[wheelNumber].alphabet[i], for: .normal)
button.setTitleColor(.black, for: .normal)
button.pin = i
button.wheelNumber = wheelNumber
button.layer.cornerRadius = 15
let length = wheels[wheelNumber].length
button.frame = CGRect(x: 0,y: 0,width: 20,height: 20)
let theda = Double.pi - Double(i) * (2.0 * Double.pi /
Double(length))
let x = button.center.x + radius * CGFloat(sin(theda)) / 2.0
let y = button.center.y + radius * CGFloat(cos(theda)) / 2.0
button.center = CGPoint(x: x + 180, y: y + 125)
button.addTarget(self, action: #selector(pressed), for: .touchUpInside)
self.view.addSubview(button)
// buttons.append(button)
}
}
class MyButton: UIButton {
var pin = 0
var verticalTitle = ""
var horizontalTitle = ""
var origionalRect = CGRect(x:0,y:0,width:0,height:0)
var wheelNumber = 0
答案 0 :(得分:0)
创建一个属性来按住按钮:
var buttons = [UIButton]()
在createButtons()
中,将您的button
添加到buttons
数组后,将其添加到超级视图中:
self.view.addSubview(button) // existing code
buttons.append(button)
是时候删除按钮了,将它们从其超级视图中删除并清空buttons
数组:
// remove the buttons from the screen
buttons.forEach { button in button.removeFromSuperview() }
// free up last reference to buttons so memory is freed
buttons = []
现在您可以创建更多按钮了:
createButtons(wheelNumber: someInt)