IOS(Swift)

IOS Collectionview Image slider

----___<<<<< 2021. 3. 9. 15:03

 

 이미지가 양옆으로 넘어가는 collectionView에 대한 예제입니다.

 

 영상은 아래에 참고하시고

 

 

class HomeCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var imgProudctPhoto: UIImageView!
}
class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
@IBOutlet weak var collectionView: UICollectionView!
var timer : Timer?
var currentCelIndex = 0
var arrProductPhotos = [
UIImage(named: "one"),
UIImage(named: "two"),
UIImage(named: "three")
]
override func viewDidLoad() {
super.viewDidLoad()
collectionView.delegate = self
collectionView.dataSource = self
startTimer()
}
func startTimer(){
timer = Timer.scheduledTimer(timeInterval: 2.5, target: self, selector: #selector(moveToNextIndex), userInfo: nil, repeats: true)
}
@objc func moveToNextIndex(){
currentCelIndex += 1
collectionView.scrollToItem(at: IndexPath(item: currentCelIndex, section: 0), at: .centeredHorizontally, animated: true)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return arrProductPhotos.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "homeCell", for: indexPath) as! HomeCollectionViewCell
cell.imgProudctPhoto.image = arrProductPhotos[indexPath.row]
return cell
}
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
}
}

'IOS(Swift)' 카테고리의 다른 글

Muti CollectionView in One ViewController  (0) 2021.03.10
Swift ScrollView  (0) 2021.03.10
IOS navagationbar items  (0) 2021.02.28
IOS navigationItem  (0) 2021.02.28
IOS navigationItem title  (0) 2021.02.28