ios - 如何为一个 Controller 设置多个 View

标签 ios swift

我正在为篮球战术制作一个应用程序。用户可以在多种战术之间进行选择,并为战术的每个球员命名。 enter image description here

我有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/

相关文章:

ios - xcode 4 仅在设备上复制一次资源

ios - 在textview ios中逐字删除而不是逐字符删除

swift - 为什么让 x 作为 Float 大小写在 Any 变量的开关中不匹配?

ios - 发送 uitableviewcell 的索引路径

ios - 使用 Realm 的 RLMArray 存储字符串数组

objective-c - UIImageJPEGRepresentation 在每个 iOS 设备上的行为是否应该相同?

ios - 如何在 SwiftUI ListView 中的项目之间创建间距?

ios - Json解析错误: use of undeclared type 'Foundation' in Swift iOS

ios - 如何使用 Swift 中的 editActionsForRowAtIndexPath 检测单元格是否被滑动到正常状态?

ios - 自动调整背景图像大小以适应所有设备和方向 Swift