简体   繁体   English

Swift iOS-如何为CollectionView的第二部分查找CGPoint x / y值

[英]Swift iOS -How to find CGPoint x/y values for CollectionView's second Section

I have a collectionView with 2 sections. 我有2部分的collectionView。 The frame of the first section will depend on the number of cells inside of it. 第一部分的框架将取决于其内部单元的数量。 Since the total number of cells ( firstSectionData.count ) in the first section will vary I need to find the CGPoint for the Second Section. 由于第一部分中的单元格总数( firstSectionData.count )会有所不同,因此我需要为第二部分找到CGPoint。 I specifically need to find out the x/y values of where the second section begins. 我特别需要找出第二部分开始的x/y值。

The frame will also be suffice because I can extract them from there. 框架也足够了,因为我可以从那里提取它们。 How can I find this out? 我如何找到这个?

var cv: UICollectionView!
let firstSectionCell = "FirstSectionCell"
let secondSectionCell = "SecondSectionCell"
let secondSectionHeaderView = "SecondSectionHeaderView"
let firstSectionData = [String]()
let secondSectionData = [String]()
let cellHeight: CGFloat = 50
let headerHeight: CGFloat = 60

override func viewDidLoad() {
    super.viewDidLoad()

    let layout = UICollectionViewFlowLayout()
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0)
    layout.scrollDirection = .horizontal

    cv = UICollectionView(frame: view.frame, collectionViewLayout: layout)
    cv.delegate = self ; cv.dataSource = self
    cv.register(FirstSectionCell.self, forCellWithReuseIdentifier: firstSectionCell)
    cv.register(SecondSectionCell.self, forCellWithReuseIdentifier: secondSectionCell)
    cv.register(SecondSectionHeaderView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: secondSectionHeaderView)
    view.addSubview(cv)
}

func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 2
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    if section == 0{
        return firstSectionData.count // varies
    }
    return secondSectionData.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    if indexPath.section == 0{
        return firstSectionCell = collectionView.dequeueReusableCell(withReuseIdentifier: self.firstSectionCell, for: indexPath) as! FirstSectionCell
    }

    return secondSectionCell = collectionView.dequeueReusableCell(withReuseIdentifier: self.secondSectionCell, for: indexPath) as! SecondSectionCell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: view.frame.width, height: self.cellHeight) // height is 50
}

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
    let headerView: UICollectionReusableView?
    if kind == UICollectionElementKindSectionHeader{
        let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: self.secondSectionHeaderView, for: indexPath) as! SecondSectionHeaderView
        headerView = header
    }
    return headerView!
}

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

    if section == 0{
        return CGSize.zero // the first section doesn't have a header
    }

    return CGSize(width: view.frame.width, height: self.headerHeight) // header height is 60 for the second section
}

To get the position of a UICollectionViewCell relative to the view, in which it's located (eg relative to your visible area): 要获取UICollectionViewCell相对UICollectionViewCell的视图的位置(例如,相对于您的可见区域):

if let theAttributes = collectionView.layoutAttributesForItem(at: IndexPath(item: 1, section: 0)) {
     let cellFrameInSuperview = collectionView.convert(theAttributes.frame, to: collectionView.superview)
     print("X of Cell is: \(cellFrameInSuperview.origin.x)")
  }

This type of information is kept by the collection view's layout manager. 此类信息由集合视图的布局管理器保留。 The collection view provides two convenience methods: 收集视图提供了两种方便的方法:

layoutAttributesForItem(at:)

and: 和:

layoutAttributesForSupplementaryElement(ofKind:at:)

These return a value of UICollectionViewLayoutAttributes? 这些返回UICollectionViewLayoutAttributes?的值UICollectionViewLayoutAttributes? . This in turn has properties such as frame and others. 这又具有诸如frame和其他属性。

If you want the frame of a specific item in a collection view you would do: 如果要在集合视图中显示特定项目的框架,请执行以下操作:

let frame = collectionView.layoutAttributesForItem(at: someIndexPath)?.frame

If you want the frame of a section header in a collection view you would do: 如果要在集合视图中显示节标题的框架,请执行以下操作:

let frame = collectionView.layoutAttributesForSupplementaryElement(ofKind: UICollectionElementKindSectionHeader, at: someSectionIndexPath)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Swift CGPoint x和y覆盖程序约束 - Swift CGPoint x and y overrides programatic constraints Swift-如何确定collectionView的scrollView.content.y? - Swift -how is a collectionView's scrollView.content.y determined? Swift - 如何以编程方式访问 collectionView 的 targetContentOffset.pointee.y - Swift -How to programmatically access collectionView's targetContentOffset.pointee.y iOS如何在CollectionView相对UIWindow中获取对象的CGPoint - iOS how get CGPoint of object in collectionview relative UIWindow 如何将CGRect转换为CGPoint iOS Swift - How to convert CGRect to CGPoint iOS Swift iOS-collectionView contentOffset中的CGPoint值不正确 - iOS - inaccurate CGPoint value in collectionView contentOffset iOS:在Swift3的UICollectionViewLayout部分中,如何访问主要的CollectionView属性? - iOS: in Swift3, in the UICollectionViewLayout section, how can I access to the main CollectionView properties? swift:collectionView部分 - swift: collectionView section 如何在 iOS、PDFKit、swift 中获取 pdf 页面的精确 X 和 Y 原点值 - How to get exact X and Y origin values of pdf page in iOS, PDFKit, swift Swift iOS-使用SegmentedControl将ChildViewController添加到CollectionView节 - Swift iOS -Adding ChildViewController to CollectionView Section using SegmentedControl
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM