我正在制作一款有2种游戏模式(redMode和blueMode)的射击游戏。这两种模式是ViewController,有subView(UIView子类)和游戏结果视图,显示你得分的数量。
但是有一些差异取决于游戏模式。我试图制作“ResultView Class”,并希望让这两种游戏模式拥有该类。但我不知道如何分开处理取决于游戏模式。
理想:
(现在我在每个游戏模式中制作“resultView”,即使内容几乎相同“)
我该如何解决?
//ViewController for RedMode
class RedGameViewController: UIViewController {
var gameStatus: status!
var redView: RedView!
override init(frame: CGRect) {
super.init(frame: frame)
redView = RedView.init(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 568))
self.view.addSubview(redView)
redView.redGameViewController = self
//.....
}
enum status: String {
case isPlaying = "isPlaying"
case isPausing = "isPausing"
case isGameOver = "isGameOver"
}
class RedView: UIView {
weak var redGameViewController: RedGameViewController!
override init(frame: CGRect) {
super.init(frame: frame)
}
//....
@objc func showResultView(){
var resultView = UIImageView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 568))
resultView.isUserInteractionEnabled = true
resultView.image = UIImage(named:"resultView_BG")!
self.addSubview(resultView)
resultScoreLabel.text = String(score)
resultBackToTopBtn.isHidden = false
if redViewController.gameStatus == .isGameOver {
print("isGameOver")
}else{
print("Clear")
}
}
}
//ViewController for BlueMode
class BlueGameViewController: UIViewController {
var gameStatus: status!
var blueView: BlueView!
override init(frame: CGRect) {
super.init(frame: frame)
blueView = BlueView.init(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 568))
self.view.addSubview(blueView)
blueView.blueGameViewController = self
//.....
}
enum status: String {
case isPlaying = "isPlaying"
case isPausing = "isPausing"
case isGameOver = "isGameOver"
}
class BlueView: UIView {
weak var blueGameViewController: BlueGameViewController!
override init(frame: CGRect) {
super.init(frame: frame)
}
//....
@objc func showResultView(){
var resultView = UIImageView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 568))
resultView.isUserInteractionEnabled = true
resultView.image = UIImage(named:"resultView_BG")!
self.addSubview(resultView)
//Different from showResultView in RedView
resultScoreLabel.text = String(score*10)
resultBackToTopBtn.isHidden = true
if blueViewController.gameStatus == .pausing {
print("isPausing")
}else{
print("Clear")
}
}
}
答案 0 :(得分:0)
这样做的好方法是使用协议。
选项1:
假设您有一个名为ColoredView的协议,其中包含您的逻辑方法
protocol ColoredView {
func showResult(/* add parameters if you need them */)
}
在名为ResultViewController的UIViewController中,您将拥有一个能够调用逻辑函数的ColoredView,如
class ResultViewController {
var coloredView: UIView!
func showResultView() {
guard let coloredView = self.coloredView as? ColoredView else {
// This is not a coloredView
return
}
coloredView.showResult()
}
}
由此,只需创建实现协议的两个视图
class RedView: UIView, ColoredView {
func showResult() {
// Insert Red logic here
}
}
class BlueView: UIView, ColoredView {
func showResult() {
// Insert Blue logic here
}
}
然后使用RedView或BlueView创建ResultViewController作为其coloredView。
选项2:
您可以使用
等协议分离变量protocol ColoredView {
func getScoreText(from score: Int) -> String
func getPrintText(for status: Status) -> String
var isHidden: Bool
}
就像选项1
一样func showResult() {
if let coloredView = self.coloredView as? ColoredView {
resultScoreLabel.text = coloredView.getScoreText(from: score)
resultBackToTopBtn.isHidden = coloredView.isHidden
print(coloredView.getPrintText(for: gameStatus)
}
}
用
class RedView: UIView, ColoredView {
func getScoreText(from score: Int) -> String {
// Insert Red logic here
}
var isHidden = false
func getPrintText(for status: Status) -> String {
// Insert Red logic here
}
}
class BlueView: UIView, ColoredView {
func getScoreText(from score: Int) -> String {
// Insert Blue logic here
}
var isHidden = true
func getPrintText(for status: Status) -> String {
// Insert Blue logic here
}
}
希望它有所帮助;)