My UIImageView is not working correctly why is that? I don’t know what to do really.
//
// ViewController.swift
// StagINC
//
// Created by - on 2022-01-08.
//
import UIKit
class ViewController: UIViewController {
@IBOutlet var collectioView: UICollectionView!
@IBOutlet var myPage: UIPageControl!
var SliderImages = [UIImage(named: "3"),UIImage(named: "4"),UIImage(named: "5"),UIImage(named: "2")]
// var SliderImages = [ "3", "4", "5"]
// var SliderImages = [UIImage(named: "2")!, UIImage(named: "2")!, UIImage(named: "2")!]
var timer = Timer()
var counter = 0
override func viewDidLoad() {
super.viewDidLoad()
collectioView.delegate = self
collectioView.dataSource = self
myPage.numberOfPages=SliderImages.count
myPage.currentPage=0
DispatchQueue.main.async {
self.timer = Timer.scheduledTimer(timeInterval: 2.0, target: self, selector: #selector(self.changeImage), userInfo: nil, repeats: true)
}
}
@objc func changeImage(){
if counter < SliderImages.count {
let index = IndexPath.init(item: counter, section: 0)
self.collectioView.scrollToItem(at: index, at: .centeredHorizontally, animated: true)
myPage.currentPage = counter
counter += 1
}else {
counter = 0
let index = IndexPath.init(item: counter, section: 0)
self.collectioView.scrollToItem(at: index, at: .centeredHorizontally, animated: true)
myPage.currentPage = counter
counter = 1
}
}
// func startTimer(){ timer = Timer.scheduledTimer(timeInterval: 2.5, target: self, selector: #selector(moveToNextIndex), userinfo: nil, repeats: true)
}
// @objc func moveToNextIndex(){ if currentCellIndex < SliderImages.count -1{ currentCellIndex += 1
// } else { currentCellIndex = 0
// } currentCellIndex += 1 collectionView.scrollToItem(at: IndexPath(item: curentCellIndex, section: 0), at: .centeredHorizontally, animated: true)
// pageControl.currentPage = currentCellIndex
extension ViewController : UICollectionViewDataSource , UICollectionViewDelegate {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
return SliderImages.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! MyCollectionCell
if let vc = cell.viewWithTag(111) as? UIImageView {
vc.image = SliderImages[indexPath.row]
// cell.myWebImage.image=UIImage(named: SliderImages[indexPath.row])
} else if let ab = cell.viewWithTag(222) as? UIPageControl {
ab.currentPage = indexPath.row
}
return cell
}
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
myPage.currentPage=indexPath.row
// func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSIZE { return CGSize(width: collectionView.frame.width, height: collectionView.frame.height.
}
// func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { return 0
}
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, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0.0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0.0
}
}