gpt4 book ai didi

ios - UIView 子类可以创建其他自定义 View 实例吗?

转载 作者:行者123 更新时间:2023-11-29 01:51:44 24 4
gpt4 key购买 nike

我有一个名为 LifeCounter 的 UIView 子类,我想为每个玩家创建一个。我能够向 Main.storyboard 添加两个 View ,通过属性面板将它们的类设置为 LifeCounter 并以这种方式创建多个实例,连接到 View Controller ,更改属性等。

我现在想的是创建一个更大的 View GameHeader,它将保存 LifeCounters 和一些其他补充信息,例如时间、游戏重置按钮等。GameHeader 是 UIView 的子类,但我无法获取它在模拟器中绘制我的 LifeCounter View ,我不知道为什么。

GameHeader 当前是一个拖入 Storyboard 中的 View ,并在属性面板中指定它的类。

GameHeader.swift

import UIKit

class GameHeader: UIView {

// MARK: Properties

// The Frame
let topFrame = CGRect(x: 0, y: 0, width: 320, height: 200)

// MARK: Initlization
required init() {
super.init(frame: topFrame)

// Adds the player one counter
let playerOneCounter = LifeCounter()
addSubview(playerOneCounter)

}

required init(coder aDecoder: NSCoder) {
// Calls the super class (UIView) initializer
super.init(coder: aDecoder)
}

}

生命计数器.swift

import UIKit

class LifeCounter: UIView {

// MARK: Propterties

// Starting life total
var lifeTotal = 20 {
didSet {
// Updates the layout whenever the lifeTotal is updated
setNeedsLayout()
}
}

// Creates the UI Labels
// All created views need a defined frame for where they sit
// Is this neccesary outside of the init? Is there a better way?
var counter = UILabel(frame: CGRect(x: 0, y: 20, width: 100, height: 90))
var playerName = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 40))
var winner = ""



// MARK: Initlization

// First init. LifeCounter takes a frame parameter, the adds the labels etc.
init() {
super.init(frame: CGRect(x: 0, y: 0, width: 200, height: 100))
self.addLifeCounter()
}

required init(coder aDecoder: NSCoder) {
// Calls the super class (UIView) initializer
super.init(coder: aDecoder)
}

func addLifeCounter() {
print("addLifeCounter is running")
// Styles life counter label
counter.textColor = UIColor.blackColor()
counter.textAlignment = .Center
counter.font = UIFont.boldSystemFontOfSize(72)
counter.text = String(lifeTotal)

// Styles playerName label
playerName.text = "Player Name"
playerName.textAlignment = .Center


// Button
let minusButton = UIButton(frame: CGRect(x: 5, y: 110, width: 40, height: 40))
let plusButton = UIButton(frame: CGRect(x: 55, y: 110, width: 40, height: 40))

minusButton.backgroundColor = UIColor.redColor()
plusButton.backgroundColor = UIColor.blueColor()

// Button action
minusButton.addTarget(self, action: "minusLife:", forControlEvents: .TouchDown)
plusButton.addTarget(self, action: "plusLife:", forControlEvents: .TouchDown)

addSubview(playerName)
addSubview(counter)
addSubview(minusButton)
addSubview(plusButton)
}


// MARK: Button actions
func minusLife(minusButton: UIButton) {
lifeTotal -= 1
counter.text = String(lifeTotal)
}

func plusLife(plusButton: UIButton) {
lifeTotal += 1
counter.text = String(lifeTotal)
}

}

最佳答案

init(coder:) 是从 Storyboard或 nib 创建 View 时调用的初始化器。如果您将创建和添加 LifeCounter 实例的代码移到那里,它应该可以工作。

使其更具可重用性的一个好策略是创建一个从两个初始化程序调用的设置方法,这样无论它来自 Storyboard/ Nib 还是以编程方式实例化,它都会运行。

class GameHeader: UIView {
let topFrame = CGRect(x: 0, y: 0, width: 320, height: 200)

required init() {
super.init(frame: topFrame)
self.setupSubviews()
}

required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.setupSubviews()
}

func setupSubviews() {
let playerOneCounter = LifeCounter()
addSubview(playerOneCounter)
}

}

关于ios - UIView 子类可以创建其他自定义 View 实例吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31327528/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com