Ios 集合视图流布局类似于Apple Store应用程序
我正在使用Ios 集合视图流布局类似于Apple Store应用程序,ios,iphone,objective-c,uicollectionview,uicollectionviewlayout,Ios,Iphone,Objective C,Uicollectionview,Uicollectionviewlayout,我正在使用UICollectionView来显示图像,就像在Apple Store iPhone应用程序中一样。(附截图) 但我无法设置类似苹果商店应用程序的流量布局。我面临的问题,在设置它的布局和内容偏移和插入。我尝试了每一件事,甚至对它的流布局进行了子类化。即使这样对我也不起作用 - (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity
UICollectionView
来显示图像,就像在Apple Store iPhone应用程序中一样。(附截图)
但我无法设置类似苹果商店应用程序的流量布局。我面临的问题,在设置它的布局和内容偏移和插入。我尝试了每一件事,甚至对它的流布局进行了子类化。即使这样对我也不起作用
- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
我还尝试为相同的应用程序实现iCarousel
,但效果并不理想
任何与流程布局相关的帮助都将不胜感激。此外,如果除了使用集合视图之外还有其他解决方案
更新:
我已经实现了以下代码来设置它的流布局:
每件事都像Appstore图像布局一样
更新:
我最终使用了iCarousel
,并对其进行了一些修改。
课堂表现
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中)
实现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)
}
func-scrollViewWillendDraging(scrollView:UIScrollView,withVelocity:CGPoint,targetContentOffset:UnsafemeutablePointer){
让pageWidth=itemSize.width
targetContentOffset.pointee=scrollView.contentOffset
var系数:CGFloat=0.5
如果速度.x<0{
因子=-因子
打印(“右”)
}否则{
打印(“左”)
}
设a:CGFloat=scrollView.contentOffset.x/pageWidth
var指数=整数(四舍五入(a+因子))
如果指数<0{
索引=0
}
如果索引>collectionViewDataList.count-1{
index=collectionViewDataList.count-1
}
设indexPath=indexPath(行:索引,节:0)
collectionView?.scrollToItem(位于:indexPath,位于:。左侧,动画:true)
}
请参阅“我的项目在过去的骑乘”部分的示例屏幕截图
我已经在collectionViewCell中实现了uicollectionview
你能告诉我们你试过什么吗?你可能很接近一个简单的错误。。。因此,真正了解您的尝试和实际结果将非常有用。@nhgrif:谢谢您的快速回复。我已经更新了我的问题。即使我认为,我错过了一些东西,但没有得到它的暗示。
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
}
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)
}