【发布时间】:2020-02-07 05:57:52
【问题描述】:
我在函数被调用 2 次时遇到问题。因此,PageView 中的值设置不正确,因为索引值不正确。如何正确控制?
ViewController.swift
class ViewController: UIViewController {
@IBOutlet weak var collectionView: UICollectionView!
@IBOutlet weak var pageView: UIPageControl!
@IBOutlet var guideView: UIView!
var step : Int! = 0
var beforeStep : Int! = 0
var imageArray = [UIImage(named: "swipe1"),
UIImage(named: "swipe2"),
UIImage(named: "swipe3"),
UIImage(named: "swipe4"),
UIImage(named: "swipe5")]
override func viewDidLoad() {
super.viewDidLoad()
pageView.numberOfPages = imageArray.count
pageView.currentPage = 0
pageView.currentPageIndicatorTintColor = UIColor.color(.dacColor)
pageView.pageIndicatorTintColor = UIColor.gray
view.backgroundColor = UIColor(red: CGFloat(252.0/255.0), green: CGFloat(251.0/255.0), blue: CGFloat(245.0/255.0), alpha: CGFloat(1.0))
collectionView.backgroundColor = UIColor(red: CGFloat(252.0/255.0), green: CGFloat(251.0/255.0), blue: CGFloat(245.0/255.0), alpha: CGFloat(1.0))
}
}
extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return imageArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
if let vc = cell.viewWithTag(111) as? UIImageView {
vc.image = imageArray[indexPath.row]
}
if let ab = guideView.viewWithTag(222) as? UIPageControl {
ab.currentPage = indexPath.row - 1
}
return cell
}
}
extension ViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let size = collectionView.frame.size
return CGSize(width: size.width, height: size.height)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
}
有问题的功能部分
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
if let vc = cell.viewWithTag(111) as? UIImageView {
vc.image = imageArray[indexPath.row]
}
if let ab = guideView.viewWithTag(222) as? UIPageControl {
ab.currentPage = indexPath.row - 1
}
return cell
}
StoryBoard 中的 CollectionView
在 Storyboard 中查看列表
我希望将 PageController 标记为与图片的位置相匹配。
有没有人和我一样的问题解决过?
【问题讨论】:
-
你需要将 UIPageControl 放在集合视图之外(在主视图中),当你滚动时,你需要使用滚动代理找到索引
-
@SiddhantNigam 我将图片更新为问题。如您所见,PageController 不在 CollectionView 中。
-
@SiddhantNigam 我应该如何使用滚动委托?告诉我答案。
-
试试 FSPagerView 很简单:github.com/WenchaoD/FSPagerView 如果你不喜欢这个试试这个,然后我会分享你收集视图的代码
标签: ios swift uicollectionview