Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

iOS UICollectionView Page Scrolling

License

NotificationsYou must be signed in to change notification settings

maximbilan/UICollectionViewHorizontalPaging

Repository files navigation

I would like to provide an example how to do a horizontal page scrolling usingUICollectionView.

alt tag

Please set upisPaggingEnabled property toYES for the collection view object. Or viaInterface Builder:

alt tag

And let’s create a custom class for the flow layout:

alt tag

Please use the following code:

import UIKitclass CenterViewFlowLayout: UICollectionViewFlowLayout {override func collectionViewContentSize() -> CGSize {// Only support single section for now.// Only support Horizontal scrolllet count = self.collectionView?.dataSource?.collectionView(self.collectionView!, numberOfItemsInSection: 0)let canvasSize = self.collectionView!.frame.sizevar contentSize = canvasSizeif self.scrollDirection == UICollectionViewScrollDirection.Horizontal {let rowCount = Int((canvasSize.height - self.itemSize.height) / (self.itemSize.height + self.minimumInteritemSpacing) + 1)let columnCount = Int((canvasSize.width - self.itemSize.width) / (self.itemSize.width + self.minimumLineSpacing) + 1)let page = ceilf(Float(count!) / Float(rowCount * columnCount))contentSize.width = CGFloat(page) * canvasSize.width}return contentSize}func frameForItemAtIndexPath(indexPath: NSIndexPath) -> CGRect {let canvasSize = self.collectionView!.frame.sizelet rowCount = Int((canvasSize.height - self.itemSize.height) / (self.itemSize.height + self.minimumInteritemSpacing) + 1)let columnCount = Int((canvasSize.width - self.itemSize.width) / (self.itemSize.width + self.minimumLineSpacing) + 1)let pageMarginX = (canvasSize.width - CGFloat(columnCount) * self.itemSize.width - (columnCount > 1 ? CGFloat(columnCount - 1) * self.minimumLineSpacing : 0)) / 2.0let pageMarginY = (canvasSize.height - CGFloat(rowCount) * self.itemSize.height - (rowCount > 1 ? CGFloat(rowCount - 1) * self.minimumInteritemSpacing : 0)) / 2.0let page = Int(CGFloat(indexPath.row) / CGFloat(rowCount * columnCount))let remainder = Int(CGFloat(indexPath.row) - CGFloat(page) * CGFloat(rowCount * columnCount))let row = Int(CGFloat(remainder) / CGFloat(columnCount))let column = Int(CGFloat(remainder) - CGFloat(row) * CGFloat(columnCount))var cellFrame = CGRectZerocellFrame.origin.x = pageMarginX + CGFloat(column) * (self.itemSize.width + self.minimumLineSpacing)cellFrame.origin.y = pageMarginY + CGFloat(row) * (self.itemSize.height + self.minimumInteritemSpacing)cellFrame.size.width = self.itemSize.widthcellFrame.size.height = self.itemSize.heightif self.scrollDirection == UICollectionViewScrollDirection.Horizontal {cellFrame.origin.x += CGFloat(page) * canvasSize.width}return cellFrame}override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? {let attr = super.layoutAttributesForItemAtIndexPath(indexPath)?.copy() as! UICollectionViewLayoutAttributes?attr!.frame = self.frameForItemAtIndexPath(indexPath)return attr}override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {let originAttrs = super.layoutAttributesForElementsInRect(rect)var attrs: [UICollectionViewLayoutAttributes]? = Array()for attr in originAttrs! {let idxPath = attr.indexPathlet itemFrame = self.frameForItemAtIndexPath(idxPath)if CGRectIntersectsRect(itemFrame, rect) {let nAttr = self.layoutAttributesForItemAtIndexPath(idxPath)attrs?.append(nAttr!)}}return attrs}}

About

iOS UICollectionView Page Scrolling

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp