在没有 IF 语句的情况下滑动时显示和隐藏 UIView?



我有以下代码可以在滑动时滚动浏览UIViews。

我的问题是,有没有更好的方法来运行每个视图而不是使用 if 语句? 因为我觉得如果我添加更多的视图层,它会变得更大。

或者有没有办法让我获得当前视图 id/model/IBOutlet,因为我觉得我可以解决这个问题。

import UIKit
class ViewController: UIViewController {
@IBOutlet weak var WalkthroughScreen1: UIView!
@IBOutlet weak var WalkthroughScreen2: UIView!
@IBOutlet weak var WalkthroughScreen3: UIView!
@IBOutlet weak var WalkthroughScreen4: UIView!
override func viewDidLoad() {
super.viewDidLoad()
WalkthroughScreen1.isHidden = false
WalkthroughScreen2.isHidden = true
WalkthroughScreen3.isHidden = true
WalkthroughScreen4.isHidden = true
// Do any additional setup after loading the view, typically from a nib.
}
@IBAction func SwipeLeft(_ sender: UISwipeGestureRecognizer) {
if WalkthroughScreen1.isHidden == false {
WalkthroughScreen1.isHidden = true
WalkthroughScreen2.isHidden = false
} else {
if WalkthroughScreen2.isHidden == false {
WalkthroughScreen2.isHidden = true
WalkthroughScreen3.isHidden = false
} else {
if WalkthroughScreen3.isHidden == false {
WalkthroughScreen3.isHidden = true
WalkthroughScreen4.isHidden = false
}
}
}
}
@IBAction func SwipeRight(_ sender: UISwipeGestureRecognizer) {
if WalkthroughScreen4.isHidden == false {
WalkthroughScreen4.isHidden = true
WalkthroughScreen3.isHidden = false
} else {
if WalkthroughScreen3.isHidden == false {
WalkthroughScreen3.isHidden = true
WalkthroughScreen2.isHidden = false
} else {
if WalkthroughScreen2.isHidden == false {
WalkthroughScreen2.isHidden = true
WalkthroughScreen1.isHidden = false
}
}
}
}
}

提前感谢您的帮助。

创建组件并用数字对它们进行编号以指示差异时,应使用数组。这是我应该做的:

  1. 卸下所有插座
  2. 使用您的视图创建 IBOutletCollection,并标记您的视图以指示层次结构
  3. 在视图DidLoad中对该IBOutletCollection进行排序,以便具有最低值的标签成为第一个,具有最大值的标签成为最后一个。这是必需的,因为IBOutletCollection的顺序没有排序,并且可能会改变,不管其他人怎么说。
  4. 将 swipeLeft 方法更改为类似这样的方法(我这里没有编译器,所以我可能会犯一些错误(:

法典:

guard let index = walkthroughScreens.index { !$0.hidden } else { return } // This gets the non-hidden walkthroughScreen
walkthroughScreens[index].isHidden = true
walkthroughScreens[index + 1].isHidden = false // <- make sure index isn't out of bounds.

试试这个(可能包含轻微的语法错误,当前没有可用的 Xcode(:

import UIKit
class ViewController: UIViewController {
@IBOutlet weak var WalkthroughScreen1: UIView!
@IBOutlet weak var WalkthroughScreen2: UIView!
@IBOutlet weak var WalkthroughScreen3: UIView!
@IBOutlet weak var WalkthroughScreen4: UIView!
weak var screens:[UIView]!
override func viewDidLoad() {
super.viewDidLoad()
screens = [WalkthroughScreen1, WalkthroughScreen2, WalkthroughScreen3, WalkthroughScreen4]
WalkthroughScreen1.isHidden = false
WalkthroughScreen2.isHidden = true
WalkthroughScreen3.isHidden = true
WalkthroughScreen4.isHidden = true
// Do any additional setup after loading the view, typically from a nib.
}
@IBAction func SwipeLeft(_ sender: UISwipeGestureRecognizer) {
if let nextInx = screens.index(of: sender.view)+1 {
if (nextInx) < screens.count {
sender.view.isHidden = true
screens[nextInx].isHidden = false
}
}
}
@IBAction func SwipeRight(_ sender: UISwipeGestureRecognizer) {
if let nextInx = screens.index(of: sender.view)-1 {
if (nextInx) > 0 {
sender.view.isHidden = true
screens[nextInx].isHidden = false
}
}
}
}
Use design widgets, controllers etc to make an interactive UI so for your question better solution would be use UIPageControl.
@IBOutlet weak var pageControl: UIPageControl!
@IBOutlet weak var mainScrl: UIScrollView!
let arrImages = [“1.png”, "2.png", "3.png”]// No. of images or you can use your views with  
func loadScrollView() {
let pageCount : CGFloat = CGFloat(arrImages.count)
mainScrl.backgroundColor = UIColor.clearColor()
mainScrl.delegate = self
mainScrl.pagingEnabled = true
mainScrl.contentSize = CGSizeMake(mainScrl.frame.size.width * pageCount, mainScrl.frame.size.height)
mainScrl.showsHorizontalScrollIndicator = false
pageControl.numberOfPages = Int(pageCount)
pageControl.addTarget(self, action: #selector(self.pageChanged), forControlEvents: .ValueChanged)
for i in 0..<Int(pageCount) {
print(self. mainScrl.frame.size.width)
let image = UIImageView(frame: CGRectMake(self. mainScrl.frame.size.width * CGFloat(i), 0, self.mainScrl.frame.size.width, self.mainScrl.frame.size.height))
image.image = UIImage(named: arrImages[i])!
image.contentMode = UIViewContentMode.ScaleAspectFit
self.mainScrl.addSubview(image)
}
}
//MARK: UIScrollView Delegate
func scrollViewDidScroll(scrollView: UIScrollView) {
let viewWidth: CGFloat = scrollView.frame.size.width
// content offset - tells by how much the scroll view has scrolled.
let pageNumber = floor((scrollView.contentOffset.x - viewWidth / 50) / viewWidth) + 1
pageControl.currentPage = Int(pageNumber)
}

//MARK: Page tap action
func pageChanged() {
let pageNumber = pageControl.currentPage
var frame = scrMain.frame
frame.origin.x = frame.size.width * CGFloat(pageNumber)
frame.origin.y = 0
scrMain.scrollRectToVisible(frame, animated: true)
}

在互联网上找不到解决方案,但下面的代码可以让你了解如何使用UICollectionView实现它:

创建一个UIViewcontrollerUICollectionView作为子视图,其中所有四个约束的值为零。

在您的UIViewcontroller中添加以下代码:

import UIKit
class ViewController: UIViewController {
let arrayTitle = ["Hi", "Hello", "Ok", "Got it"]
let arrayColors = [UIColor.red,UIColor.blue,UIColor.black,UIColor.gray]
@IBOutlet weak var collectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.collectionView!.register(UINib(nibName: "CustomCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "cellReuseIdentifier")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.size.width, height: collectionView.frame.size.height)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 4
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellReuseIdentifier", for: indexPath) as! CustomCollectionViewCell
cell.labelTitle.text = arrayTitle[indexPath.row]
cell.backgroundColor = arrayColors[indexPath.row]
return cell
} 
}

创建一个CustomCollectionViewCell并添加以下代码行:

class CustomCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var labelTitle: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
}

希望您能了解如何实现这一点。我认为你可以自己处理故事板。 如果您对故事板、UI 实现和约束有任何疑问,请随时询问。

最新更新