ios - 单击按钮时 SWIFT 删除 UIButton

标签 ios swift uibutton

使用 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/

相关文章:

ios - Xcode12.4 - 带有 xcframework 子项目的 iOS Umbrella xcframework 框架

ios - 仅在 TableView 的特定单元格上显示和隐藏 View

ios - 未调用 UIButton 目标操作

Swift 多线程 Getter

ios - swift 3 : Cannot automatically unwrap optional before setting it to Label

iphone - 单击上一个按钮后如何在单选按钮上保持选择

ios - 如何在没有偏移的情况下替换 Storyboard 中按钮的图像和标题?

ios - NSData 不接受有效的 base64 编码字符串

iphone - 通过导航栏按钮的操作在 UITableview 中设置删除按钮

ios - 测量在 iOS 中内嵌播放的嵌入式 YouTube 视频的音频电平