简体   繁体   English

通过单击按钮显示或隐藏项目

[英]Show or hide items by clicking button

I have four imageview contents in an XIB and a button that covers all my XIB. 我在XIB中有四个imageview内容,一个按钮覆盖了我的所有XIB。 I want to make when the user tap the button, the first imageview is shown, the next tap is hidden and the second imageview is displayed and so on until all my imageview is shown / hidden. 我想在用户点击按钮时进行显示,显示第一个imageview,隐藏下一个点击,显示第二个imageview,依此类推,直到显示/隐藏我的所有imageview。 What would be the most efficient way to do it? 什么是最有效的方法?

Save all your UIImageViews to an array , and current showing imageView to a variable, it may look like this: 将所有UIImageViews保存到一个array ,并将当前显示的imageView到一个变量中,它看起来可能像这样:

var imageViews: [UIImageView] = []

var currentImageViewIndex = 0 {
    didSet {
        if currentImageViewIndex >= imageViews.count { currentImageViewIndex = 0 }
        imageViews[oldValue].isHidden = true
        imageViews[currentImageViewIndex].isHidden = false
    }
}

func handleTap() {
    currentImageViewIndex += 1
}

I suggest you use a state variable that contains an enum listing the various states (firstImageVisible, secondImage.... ) then you can have a function inside the enum that switches to the nextState (being the target of your button action) you can also easily iterate through states of an enum, check the documentation for the CaseIterable protocol. 我建议您使用一个状态变量,该变量包含一个列出各种状态的枚举(firstImageVisible,secondImage ....),然后您可以在枚举内使用一个函数,该函数可以切换到nextState(成为按钮操作的目标),也可以轻松遍历枚举的状态,请查看CaseIterable协议的文档。 Often having a property observer (didSet) on the state is a handy place to update other parts of the UI which need to change every time the state changes. 通常,在状态上有一个属性观察器(didSet)是一个方便的地方,用于更新每次状态更改时都需要更改的UI的其他部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM