如何为一个控制器

时间:2016-01-25 17:06:13

标签: ios swift

我正在制作一个篮子战术的应用程序。用户可以选择多种战术,并为战术的每个玩家命名。 enter image description here

我有超过30种战术。每种战术之间唯一改变的是球员在场上的位置(x和y)。战术只是一个带有背景图像的视图和每个玩家的UIViews。 我想知道用同一个控制器管理每个策略(视图)的最佳方法是什么。

2 个答案:

答案 0 :(得分:2)

我会通过为您的战术视图创建子类来以编程方式创建它们,可以用来显示所有不同的玩家位置。

我还建议您为播放器视图创建一个子类,因为它们看起来与标签文本和定位完全相同。首先,您应该创建一个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 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]
        }
    }

}

此子类包含玩家视图的数组(视图本身可以在战术中重复使用,但如果您不想重复使用它们,可以初始化多个战术视图。)

它还允许您通过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
    }

}

答案 1 :(得分:1)

很多有可能实现您的目标。一个是:

1-为您的策略选择持久性方法,因为您需要一个存储和/或检索数据的地方。您可以通过任何iOS持久性源(CoreData,plist,从JSON获取等等)来管理玩家视图的位置取决于您打算如何处理最终应用程序。

2 - 在您的视图控制器上,您可以同时在NSArray或NSDictionary上加载所有30种策略(再次取决于您希望实现的目标)。

3 - 代表tableView,CollectionView或用户可以选择的任何UI的策略。

4 - 向用户展示策略,我认为最好的方法是通过CoreAnimation改变玩家视图的位置,因为用户选择不同的策略。 http://www.raywenderlich.com上有很好的动画教程。

希望能帮助您找到最佳解决方案。