ios - Collection view Flow layout 类似于 Apple Store Application

标签 ios iphone objective-c uicollectionview uicollectionviewlayout

我正在使用 UICollectionView 来显示图像,就像在 Apple Store iPhone Application 中一样。 (附截图)

但我无法设置类似于 Apple store 应用程序的流布局。我在设置其布局和内容偏移量和插图时遇到问题。我尝试了每一件事,甚至将它的流布局子类化。即使那样对我也不起作用。

- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect

我也尝试过实现 iCarousel ,但效果不佳。

感谢任何与流程布局相关的帮助。另外,如果除了使用 Collection View 之外还有其他解决方案。

enter image description here

enter image description here

enter image description here

更新:

我已经实现了以下代码来使用 NDCollectionViewFlowLayout 设置它的流布局:

NDCollectionViewFlowLayout *nda = [[NDCollectionViewFlowLayout alloc] init];
nda.itemSize = CGSizeMake(251, 443);
nda.sectionInset = UIEdgeInsetsMake(0.0, 0.0, 0.0, 0.0);
nda.minimumLineSpacing = 15.0;
nda.minimumInteritemSpacing = 10.0;
nda.scrollDirection = UICollectionViewScrollDirectionHorizontal;

一切都像 Appstore 图像布局。

更新:

我最终使用了 iCarousel,并做了一些修改。

最佳答案

Example project here

在类里面

     var itemSize = CGSize(width: 0, height: 0)
     @IBOutlet weak var collectionViewHeight: NSLayoutConstraint!
     @IBOutlet weak var collectionView: UICollectionView!
     fileprivate let sectionInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)

在 viewDidLoad 或 awakeFormNib 中(如果在 collectionViewCell 或 tableViewCell 中)

    if let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
    }
    collectionView.contentInset = UIEdgeInsets(top: 0, left: 16, bottom: 0, right: 8)
    collectionView.isPagingEnabled = false
    layoutIfNeeded()

    let width = collectionView.bounds.size.width-24
    let height = width * (3/4)
    itemSize = CGSize(width: width, height: height)
    layoutIfNeeded()

实现 UICollectionViewDelegateFlowLayout

    func collectionView(_ collectionView: UICollectionView,
                    layout collectionViewLayout: UICollectionViewLayout,
                    insetForSectionAt section: Int) -> UIEdgeInsets {

        return sectionInsets
    }

    func collectionView(_ collectionView: UICollectionView,
                    layout collectionViewLayout: UICollectionViewLayout,
                    sizeForItemAt indexPath: IndexPath) -> CGSize {
        return itemSize
    }

实现 ScrollViewDelegate

func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
    let pageWidth = itemSize.width
    targetContentOffset.pointee = scrollView.contentOffset
    var factor: CGFloat = 0.5
    if velocity.x < 0 {
        factor = -factor
        print("right")
    } else {
        print("left")
    }

    let a:CGFloat = scrollView.contentOffset.x/pageWidth
    var index = Int( round(a+factor) )
    if index < 0 {
        index = 0
    }
    if index > collectionViewDataList.count-1 {
        index = collectionViewDataList.count-1
    }
    let indexPath = IndexPath(row: index, section: 0)
    collectionView?.scrollToItem(at: indexPath, at: .left, animated: true)
}

在“过去的游乐设施”部分查看我的项目的示例屏幕截图 我在 collectionViewCell 中实现了 uicollectionview

enter image description here

关于ios - Collection view Flow layout 类似于 Apple Store Application,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21797879/

相关文章:

ios - 是否可以在不显示 MPMediaPickerController 的情况下显示 ios 中的所有歌曲

objective-c - 删除 NSMutableURLRequest 中的 HTTP header

ios - 禁用 UIWebView 超链接

ios - Adobe AIR Stage.contentscaleFactor 在 iPhone 4s 和 iPad mini 上始终为 1

ios - AVAudioPlayer 初始化与 web URL 崩溃

ios - 导航 Segue 无法正常工作 iOS Swift

ios - 当原始类不可用时如何解码对象?

iphone - 发生中断怎么办

iOS NSStream 套接字读取 TCP 不稳定和截断

objective-c - 如何在 mac osx 中为文件类型设置自定义图标