Skip to content

Latest commit

History

7 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

#Custom UICollectionView

Create custom UICollectionViewLayoutWith with a custom paging size of UICollectionViewCell by overriding scrollViewWillEndDragging

alt text

letcollectionMargin=CGFloat(16)letitemSpacing=CGFloat(10)letitemHeight=CGFloat(322)varitemWidth=CGFloat(0)varcurrentItem=0

1 Custom CollectionViewLayout

overridefunc viewDidLoad(){
super.viewDidLoad()letlayout:UICollectionViewFlowLayout=UICollectionViewFlowLayout()
itemWidth =UIScreen.main.bounds.width - collectionMargin *2.0
layout.sectionInset =UIEdgeInsets(top:0, left:0, bottom:0, right:0)
layout.itemSize =CGSize(width: itemWidth, height: itemHeight)
layout.headerReferenceSize =CGSize(width: collectionMargin, height:0)
layout.footerReferenceSize =CGSize(width: collectionMargin, height:0)
layout.minimumLineSpacing = itemSpacing
layout.scrollDirection =.horizontal
collectionView!.collectionViewLayout = layout
collectionView?.decelerationRate = UIScrollViewDecelerationRateFast
}

2 Custom Paging size

func scrollViewWillEndDragging(_ scrollView:UIScrollView, withVelocity velocity:CGPoint, targetContentOffset:UnsafeMutablePointer<CGPoint>){letpageWidth=Float(itemWidth + itemSpacing)lettargetXContentOffset=Float(targetContentOffset.pointee.x)letcontentWidth=Float(collectionView!.contentSize.width )varnewPage=Float(self.pageControl.currentPage)if velocity.x ==0{
newPage =floor((targetXContentOffset - Float(pageWidth)/2)/ Float(pageWidth))+1.0}else{
newPage =Float(velocity.x >0?self.pageControl.currentPage +1:self.pageControl.currentPage -1)if newPage <0{
newPage =0}if(newPage > contentWidth / pageWidth){
newPage =ceil(contentWidth / pageWidth)-1.0}}self.pageControl.currentPage =Int(newPage)letpoint=CGPoint(x:CGFloat(newPage * pageWidth), y: targetContentOffset.pointee.y)
targetContentOffset.pointee = point
}

About

Custom CollectionViewLayout with a custom paging size by overriding scrollViewWillEndDragging

Resources

Stars

105 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages