编程方式添加的UIButton不会消失切换场景
问题描述:
我使用一个游戏程序模板斯威夫特3后,当我从我的转变从“开始”屏幕“开始”屏幕上我的“游戏”的场景,按钮不会消失。 我读过其他人的帖子,但没有任何帮助。我的按钮是一个编程添加的UIButton有uibezierpath圆角矩形按钮背后使它看起来不错。问题是,它(按钮,UIBezierpath)不会消失,当我改变场景 - 它在完全相同的斑点为“开始”屏幕。 我的按钮代码与UIBezierpath:编程方式添加的UIButton不会消失切换场景
let playAgain = UIButton()
playAgain.frame = CGRect(x: 225, y: 247, width: 115, height: 36)
playAgain.backgroundColor = SKColor.lightGray
playAgain.setTitle("Play", for: .normal)
playAgain.setTitleColor(.black, for: .normal)
self.view?.addSubview(playAgain)
playAgain.addTarget(self, action: #selector(playAgainTapped(_:)), for: .touchUpInside)
//now for the bezierpath/ rounded rect
//let doYourPath = UIBezierPath(rect: CGRect(x: 20, y: 20, width: 100, height: 36))
//this also works
let roundRect = UIBezierPath(roundedRect: CGRect(x: 218, y: 240, width: 130, height: 50), cornerRadius: 18)
let layer = CAShapeLayer()
layer.path = roundRect.cgPath
layer.strokeColor = UIColor.black.cgColor
layer.fillColor = UIColor.lightGray.cgColor
self.view?.layer.addSublayer(layer)
func playAgainTapped(_ sender: Any?) -> Void {
print("***********")
backToGame()
}
切换场景代码:
func backToGame(){
removeAllChildren()
run(SKAction.sequence([
SKAction.wait(forDuration: 3.0),
SKAction.run() {
// 5
let reveal = SKTransition.flipHorizontal(withDuration: 0.5)
let scene = GameScene(size: self.size)
self.view?.presentScene(scene, transition:reveal)
}
]))
}
任何想法?
答
您在同一视图按钮的上海华展示现场。
由于现场是独立于那些在现场的意见,你的按钮将保持不变,所以如果你想将它移走你应该明确地删除的按钮。
声明按钮和圆角矩形全局和从其上海华/ superlayer删除它们在backToGame
。
let playAgain = UIButton()
let layer = CAShapeLayer()
func backToGame(){
removeAllChildren()
playAgain.removeFromSuperview()
layer.removeFromSuperlayer()
run(SKAction.sequence([
SKAction.wait(forDuration: 3.0),
SKAction.run() {
// 5
let reveal = SKTransition.flipHorizontal(withDuration: 0.5)
let scene = GameScene(size: self.size)
self.view?.presentScene(scene, transition:reveal)
}
]))
}
+0
非常感谢它!你有关于意见和这类东西的好文章的建议吗? –
'removeAllChildren'做了什么? – the4kman
@ the4kman它会从按钮上的文本。 –