我会通过为您的战术视图创建一个子类以编程方式创建它们,该子类可用于显示您所有不同的球员位置。
我还建议您为播放器视图创建一个子类,因为除了标签文本和位置之外,它们似乎是相同的。所以首先,你应该创建一个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 创建一个子类。您希望如何存储每个玩家视图的位置完全取决于您,但在此示例中,我使用 2D 快速数组来表示它们。您可能希望将它们转换为更易于管理的格式(例如 .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]
}
}
}
该子类包含您的玩家视图数组(视图本身可以跨战术重复使用,但如果您不想重复使用它们可以初始化多个战术视图)。
它还允许您通过tacticIndex 属性在策略之间进行更改,这会将视图的定位更改为给定的策略索引。
最后,您要初始化并将其添加到您的主视图控制器:
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
}
}