ios - Collection View 流布局类似于 Apple Store 应用程序
<p><p>我正在使用 <code>UICollectionView</code> 来显示图像,就像在 Apple Store iPhone Application 中一样。 (附截图)</p>
<p>但我无法设置类似于 Apple 商店应用的流程布局。我在设置它的布局和内容 offset 和 inset 时遇到问题。我尝试了每一件事,甚至将它的流布局子类化。即使那样对我也不起作用。</p>
<pre><code>- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
</code></pre>
<p>我也尝试实现 <code>iCarousel</code> ,但效果不佳。</p>
<p>感谢任何与流布局相关的帮助。另外,如果除了使用 Collection View 之外还有其他解决方案。</p>
<p> <img src="/image/M4GJw.jpg" alt="enter image description here"/> </p>
<p> <img src="/image/0n8pC.jpg" alt="enter image description here"/> </p>
<p> <img src="/image/Gz1FD.jpg" alt="enter image description here"/> </p>
<p><strong>更新:</strong></p>
<p>我已经实现了以下代码来使用 <a href="https://gist.github.com/nicksnyder/4075682" rel="noreferrer noopener nofollow">NDCollectionViewFlowLayout</a> 设置它的流布局:</p>
<pre><code>NDCollectionViewFlowLayout *nda = [ 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;
</code></pre>
<p>一切都像 Appstore 图像布局。</p>
<p><strong>更新:</strong></p>
<p>我最终使用了<code>iCarousel</code>,并在其中做了一些修改。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p> <a href="https://github.com/danya0365/appstore-collectionview-swift" rel="noreferrer noopener nofollow">Example project here</a> </p>
<p>在类属性中</p>
<pre><code> 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)
</code></pre>
<p>在 viewDidLoad 或 awakeFormNib 中(如果在 collectionViewCell 或 tableViewCell 中)</p>
<pre><code> 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()
</code></pre>
<p>实现 UICollectionViewDelegateFlowLayout</p>
<pre><code> 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
}
</code></pre>
<p>实现 ScrollViewDelegate</p>
<pre><code>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)
}
</code></pre>
<p>在过去的游乐设施部分查看我的项目的示例屏幕截图
我在collectionViewCell中实现了uicollectionview</p>
<p> <a href="/image/Pysac.png" rel="noreferrer noopener nofollow"><img src="/image/Pysac.png" alt="enter image description here"/></a> </p></p>
<p style="font-size: 20px;">关于ios -Collection View 流布局类似于 Apple Store 应用程序,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/21797879/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/21797879/
</a>
</p>
页:
[1]