使用 SWiFT SpriteKit 我想在按下“buttonPressedLeftFirst”和“buttonPressedRightFirst”时从屏幕上删除“createLeftButtonFirst”和“createLeftButtonFirst”按钮。你有机会帮我解决这个问题吗?
代码如下:
func createLeftButton () {
let button = UIButton();
button.frame = CGRectMake(self.frame.width - self.frame.width, 0, self.frame.width / 2, self.frame.height) // X, Y, width, height
button.backgroundColor = UIColor.greenColor()
button.addTarget(self, action: "buttonPressedRight:", forControlEvents: .TouchUpInside)
self.view!.addSubview(button)
button.layer.zPosition = 0
}
func createRightButton () {
let buttonRight = UIButton();
buttonRight.frame = CGRectMake(self.frame.width / 2, 0, self.frame.width / 2, self.frame.height) // X, Y, width, height
buttonRight.backgroundColor = UIColor.blueColor()
buttonRight.addTarget(self, action: "buttonPressedLeft:", forControlEvents: .TouchUpInside)
self.view!.addSubview(buttonRight)
buttonRight.layer.zPosition = 0
}
func createLeftButtonFirst () {
let buttonFirst = UIButton();
buttonFirst.frame = CGRectMake(self.frame.width - self.frame.width, 0, self.frame.width / 2, self.frame.height) // X, Y, width, height
buttonFirst.backgroundColor = UIColor.grayColor()
buttonFirst.addTarget(self, action: "buttonPressedRightFirst:", forControlEvents: .TouchUpInside)
self.view!.addSubview(buttonFirst)
buttonFirst.layer.zPosition = 1
}
func createRightButtonFirst () {
let buttonRightFirst = UIButton();
buttonRightFirst.frame = CGRectMake(self.frame.width / 2, 0, self.frame.width / 2, self.frame.height) // X, Y, width, height
buttonRightFirst.backgroundColor = UIColor.blackColor()
buttonRightFirst.addTarget(self, action: "buttonPressedLeftFirst:", forControlEvents: .TouchUpInside)
self.view!.addSubview(buttonRightFirst)
buttonRightFirst.layer.zPosition = 1
}
func buttonPressedRight(sender: UIButton!) {
if buttonPressed == 0 {
player.physicsBody?.applyImpulse(CGVector(dx: -15, dy: 0))
buttonPressed = 1
}
}
func buttonPressedLeft(sender: UIButton!) {
if buttonPressed == 1 {
player.physicsBody?.applyImpulse(CGVector(dx: 15, dy: 0))
buttonPressed = 0
}
}
/* First Buttons*/
func buttonPressedRightFirst(sender: UIButton!){
player.physicsBody?.applyImpulse(CGVector(dx: -15, dy: 0))
buttonPressed = 1
}
func buttonPressedLeftFirst(sender: UIButton!){
player.physicsBody?.applyImpulse(CGVector(dx: 15, dy: 0))
buttonPressed = 0
self.removeFromParent()
}
最佳答案
这个功能可以通过在它们被按下时隐藏
来实现:
func leftButtonPressed(sender: UIButton!) {
leftButton.hidden = true
rightButton.hidden = true
}
func rightButtonPressed(sender: UIButton!) {
leftButton.hidden = true
rightButton.hidden = true
}
关于ios - 单击按钮时 SWIFT 删除 UIButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36191500/