[英]UICollectionView - vertical scrollling, horizontal paging with custom layout
[英]UICollectionView custom horizontal paging layout
[![collectionview示例] [1]] [1]
我正在嘗試使用集合視圖來模擬這種行為。 我從使用這種方法開始,它使我更加接近。 盡管在水平分頁CollectionView上向右滑動時,內容向左移。 單元之間的間距也關閉。 我相信它需要自定義布局,但不確定。
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSize(width: collectionView.frame.width - 20, height: collectionView.frame.height - 20)
}
它取決於3個因素1)截面插圖2)單元間距3)單元大小
每個方面的任何更改都必須針對您的情況更改其他
1)左右設置20
2)將像元間距設置為10
func collectionView(collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
return 10
}
func collectionView(collectionView: UICollectionView, layout
collectionViewLayout: UICollectionViewLayout,
minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
return 10
}
3)設置像元大小
func collectionView(collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSize(width: collectionView.frame.width / 1.15 ,height: collectionView.frame.height)
}
4)這將使屏幕中的單元格居中
func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
let pageWidth:CGFloat = scrollView.frame.width / 1.15 + cellSpacing ;
let currentOffset:CGFloat = scrollView.contentOffset.x;
let targetOffset:CGFloat = targetContentOffset.memory.x
var newTargetOffset:CGFloat = 0;
if targetOffset > currentOffset
{
newTargetOffset = ceil(currentOffset / pageWidth) * pageWidth;
}
else
{
newTargetOffset = floor(currentOffset / pageWidth) * pageWidth;
}
if newTargetOffset < 0
{
newTargetOffset = 0
}
else if newTargetOffset > scrollView.contentSize.width
{
newTargetOffset = scrollView.contentSize.width;
}
targetContentOffset.memory.x = currentOffset
scrollView.setContentOffset(CGPointMake(newTargetOffset, 0), animated: true)
}
我對@Mohamed的解決方案進行了一些更改,它對我來說非常有效:
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
let cellSpacing: CGFloat = self.collectionView(self.collectionView, layout: self.collectionView.collectionViewLayout, minimumInteritemSpacingForSectionAt: 0)
let pageWidth: CGFloat = self.pageWidth + cellSpacing
let currentOffset = scrollView.contentOffset.x
let targetOffset = targetContentOffset.pointee.x
targetContentOffset.pointee.x = currentOffset
var pageNumber = 0
if targetOffset > currentOffset {
pageNumber = Int(ceil(currentOffset / pageWidth))
}
else {
pageNumber = Int(floor(currentOffset / pageWidth))
}
let indexPath = IndexPath(row: pageNumber, section: 0)
self.collectionView.scrollToItem(at: indexPath, at: UICollectionView.ScrollPosition.centeredHorizontally, animated: true)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.