Hallo Ich habe UICollectionView
horizontale Bild Auflistungen Ich möchte PageControl
hinzufügen, wenn Bilder scrollen, zeigt ich pagecontrol Selektor und IBOutlet
, aber, wie kann ich es integrieren? zwischen UICollecitonView
. ?So fügen Sie PageControl in UICollectionView Image Scrolling
Meine Codes unten.
class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
@IBOutlet weak var View : DesignableView!
@IBOutlet var collectionView: UICollectionView!
@IBOutlet var collectionViewLayout: UICollectionViewFlowLayout!
@IBOutlet open weak var pageControl: UIPageControl? {
didSet {
pageControl?.addTarget(self, action: #selector(ViewController.pageChanged(_:)), for: .valueChanged)
}
}
override func viewDidLoad() {
super.viewDidLoad()
pageControl?.numberOfPages = 11
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 11;
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell: ImageCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "ImageCollectionViewCell", for: indexPath) as! ImageCollectionViewCell
cell.label.text = "Cell \(indexPath.row)"
cell.backgroundImageView.image = UIImage(named: "Parallax \(indexPath.row + 1)")
// Parallax cell setup
cell.parallaxTheImageViewScrollOffset(self.collectionView.contentOffset, scrollDirection: self.collectionViewLayout.scrollDirection)
return cell
}
@IBAction open func pageChanged(_ sender: UIPageControl) {
print(sender.currentPage)
}
// MARK: Delegate
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return collectionView.bounds.size;
}
// MARK: Scrolling
func scrollViewDidScroll(_ scrollView: UIScrollView) {
// Parallax visible cells
for cell: ImageCollectionViewCell in collectionView.visibleCells as! [ImageCollectionViewCell] {
cell.parallaxTheImageViewScrollOffset(self.collectionView.contentOffset, scrollDirection: self.collectionViewLayout.scrollDirection)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Geck arbeiten nicht mit Pagecontrol, wenn ich func Collection hinzugefügt (_ Collection: UICollectionView, numberOfItemsInSection Abschnitt: Int) -> Int { return self .myarray.count // HIER funktioniert nicht page control } – SwiftDeveloper
Das ist, weil, um eine Seite für jedes Element zu erstellen, müssen Sie Abschnitte, nicht Zeilen verwenden –
Aber mit dieser Lösung, wenn der Benutzer auf die nächste Seite und dann wischt Wischen Sie zurück, ohne den Finger zu heben, das PageControl zeigt die falsche Seite an. Ich denke, Sie sollten @ Lukes Antwort in Betracht ziehen – gasparuff