swift iOS操作系统|雨燕|选择顶部菜单选项时隐藏其他视图

kzmpq1sx  于 2022-12-02  发布在  Swift
关注(0)|答案(1)|浏览(102)

我目前有6个视图在一个视图控制器与一个按钮,以选择哪个视图显示和隐藏。

@IBOutlet weak var liveScrollView: UIScrollView!
@IBOutlet weak var sevenDayCollectionView: UICollectionView!
@IBOutlet weak var fourTeenDayCollectionView: UICollectionView!
@IBOutlet weak var thirtyCollectionView: UICollectionView!
@IBOutlet weak var ninetyCollectionView: UICollectionView!
@IBOutlet weak var allDataCollectionView: UICollectionView!

使用按钮,我控制selectedIndex,并且selectedIndex隐藏和显示。

var selectedIndex = 0 {
        didSet {
            switch selectedIndex {
            case 0:
                liveScrollView.isHidden = false
                sevenDayCollectionView.isHidden = true
                fourTeenDayCollectionView.isHidden = true
                thirtyCollectionView.isHidden = true
                ninetyCollectionView.isHidden = true
                allDataCollectionView.isHidden = true
            case 1:
                liveScrollView.isHidden = true
                sevenDayCollectionView.isHidden = false
                fourTeenDayCollectionView.isHidden = true
                thirtyCollectionView.isHidden = true
                ninetyCollectionView.isHidden = true
                allDataCollectionView.isHidden = true
            case 2:
                liveScrollView.isHidden = true
                sevenDayCollectionView.isHidden = true
                fourTeenDayCollectionView.isHidden = false
                thirtyCollectionView.isHidden = true
                ninetyCollectionView.isHidden = true
                allDataCollectionView.isHidden = true
            case 3:
                liveScrollView.isHidden = true
                sevenDayCollectionView.isHidden = true
                fourTeenDayCollectionView.isHidden = true
                thirtyCollectionView.isHidden = false
                ninetyCollectionView.isHidden = true
                allDataCollectionView.isHidden = true
            case 4:
                liveScrollView.isHidden = true
                sevenDayCollectionView.isHidden = true
                fourTeenDayCollectionView.isHidden = true
                thirtyCollectionView.isHidden = true
                ninetyCollectionView.isHidden = false
                allDataCollectionView.isHidden = true
            case 5:
                liveScrollView.isHidden = true
                sevenDayCollectionView.isHidden = true
                fourTeenDayCollectionView.isHidden = true
                thirtyCollectionView.isHidden = true
                ninetyCollectionView.isHidden = true
                allDataCollectionView.isHidden = false
            default:
                break
            }
        }

这似乎是一个很长的路,有没有更好的方法做到这一点?
我所能想到的是将所有视图放在一个scrollView中,并在用户更改selectedIndex时滚动内容。这是解决这个问题的唯一和最好的方法吗?

wnavrhmk

wnavrhmk1#

有很多种写法--这里有一种:

var selectedIndex = 0 {
    didSet {
        let views: [UIView] = [
            liveScrollView, sevenDayCollectionView,
            fourTeenDayCollectionView, thirtyCollectionView,
            ninetyCollectionView, allDataCollectionView,
        ]
        for (i, v) in views.enumerated() {
            v.isHidden = i != selectedIndex
        }
    }
}

您甚至可以将该数组放在类级别:

let views: [UIView] = [
    liveScrollView, sevenDayCollectionView,
    fourTeenDayCollectionView, thirtyCollectionView,
    ninetyCollectionView, allDataCollectionView,
]

override func viewDidLoad() {
    // normal stuff
}

然后将didSet缩短为:

var selectedIndex = 0 {
    didSet {
        for (i, v) in views.enumerated() {
            v.isHidden = i != selectedIndex
        }
    }
}

这样做的好处是,您可能希望在其他地方循环查看该视图数组。

相关问题