我正在为篮球战术制作一个应用程序。用户可以在多种战术之间进行选择,并为战术的每个球员命名。
我有30多个战术。每种战术之间唯一不同的是球员在地面上的位置(x 和 y)。战术只是一个带有背景图像的 View 和每个玩家的 UIViews。 我想知道用同一个 Controller 管理每个策略( View )的最佳方法是什么。
最佳答案
我会通过为您的战术 View 创建一个子类来以编程方式创建它们,该子类可用于显示您所有不同的球员位置。
我还建议您为您的播放器 View 创建一个子类,因为除了标签文本和定位之外它们看起来是相同的。所以首先,您应该创建一个 PlayerView
子类:
class PlayerView : UIView {
override init(frame: CGRect) {
super.init(frame: frame)
// do custom setup for the player view (add label etc)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
然后您要为 TacticView
创建一个子类。你想如何为每个玩家 View 存储你的位置完全取决于你,但在这个例子中,我使用了一个 2D swift 数组来表示它们。您可能希望将它们移动到更易于管理的格式(例如 .plist
文件)。
您可能还想根据屏幕尺寸定义它们,以避免使用约束。例如CGPoint(x: 0.3, y: 0.5)
(那么你只需要在设置位置时将它乘以屏幕尺寸)。
class TacticView : UIView {
let playerViewPositions : [[CGPoint]] = [ // store all your positions in a 2D array
[CGPoint(x: 10, y: 20), CGPoint(x: 10, y: 20), CGPoint(x: 10, y: 20), CGPoint(x: 10, y: 20), CGPoint(x: 10, y: 20), CGPoint(x: 10, y: 20)], // tactic 0
[CGPoint(x: 10, y: 20), CGPoint(x: 10, y: 20), CGPoint(x: 10, y: 20), CGPoint(x: 10, y: 20), CGPoint(x: 10, y: 20), CGPoint(x: 10, y: 20)], // tactic 1
[CGPoint(x: 10, y: 20), CGPoint(x: 10, y: 20), CGPoint(x: 10, y: 20), CGPoint(x: 10, y: 20), CGPoint(x: 10, y: 20), CGPoint(x: 10, y: 20)], // etc
[CGPoint(x: 10, y: 20), CGPoint(x: 10, y: 20), CGPoint(x: 10, y: 20), CGPoint(x: 10, y: 20), CGPoint(x: 10, y: 20), CGPoint(x: 10, y: 20)],
[CGPoint(x: 10, y: 20), CGPoint(x: 10, y: 20), CGPoint(x: 10, y: 20), CGPoint(x: 10, y: 20), CGPoint(x: 10, y: 20), CGPoint(x: 10, y: 20)],
[CGPoint(x: 10, y: 20), CGPoint(x: 10, y: 20), CGPoint(x: 10, y: 20), CGPoint(x: 10, y: 20), CGPoint(x: 10, y: 20), CGPoint(x: 10, y: 20)],
[CGPoint(x: 10, y: 20), CGPoint(x: 10, y: 20), CGPoint(x: 10, y: 20), CGPoint(x: 10, y: 20), CGPoint(x: 10, y: 20), CGPoint(x: 10, y: 20)],
[CGPoint(x: 10, y: 20), CGPoint(x: 10, y: 20), CGPoint(x: 10, y: 20), CGPoint(x: 10, y: 20), CGPoint(x: 10, y: 20), CGPoint(x: 10, y: 20)],
[CGPoint(x: 10, y: 20), CGPoint(x: 10, y: 20), CGPoint(x: 10, y: 20), CGPoint(x: 10, y: 20), CGPoint(x: 10, y: 20), CGPoint(x: 10, y: 20)],
[CGPoint(x: 10, y: 20), CGPoint(x: 10, y: 20), CGPoint(x: 10, y: 20), CGPoint(x: 10, y: 20), CGPoint(x: 10, y: 20), CGPoint(x: 10, y: 20)],
]
var tacticIndex:Int = 0 {
didSet { // reload views when the index value changes
self.reloadViews()
}
}
var playerArray = NSArray()
convenience init(frame:CGRect, tacticIndex:Int) {
self.init(frame: frame)
self.tacticIndex = tacticIndex
}
override init(frame: CGRect) {
super.init(frame: frame)
// do further setup (add background etc)
let arr = NSMutableArray()
for _ in 0..<6 {
let playerView = PlayerView(frame: CGRect(origin: CGPointZero, size: CGSize(width:20, height:10)))
arr.addObject(playerView)
addSubview(playerView)
}
playerArray = arr.copy() as! NSArray
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
func reloadViews() {
for i in 0..<playerArray.count {
let p = playerArray[i] as! PlayerView
p.center = playerViewPositions[tacticIndex][i]
}
}
}
此子类包含您的玩家 View 数组( View 本身可以跨战术重复使用,但如果您不想重复使用它们,您可以初始化多个战术 View )。
它还允许您通过 tacticIndex
属性在策略之间进行更改,这会将 View 的定位更改为给定的策略索引。
最后,你想初始化并将其添加到你的主视图 Controller :
class ViewController: UIViewController {
let tactic = TacticView(frame: UIScreen.mainScreen().bounds, tacticIndex:0)
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(tactic)
}
func changeTacticIndex(index:Int) {
tactic.tacticIndex = index
}
}
关于ios - 如何为一个 Controller 设置多个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34998176/