How to get position of selected item of collection view?

▼魔方 西西 提交于 2021-02-08 08:23:37

问题


In my project there is a grid of users. I made grid using collection view. When user taps on cell I need position or frame of selected cell. I have used touch event of view but it's not working in the case of collection view.

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let touch = touches.first!
    let location = touch.location(in: self.view)
    print(location)
}

also this method works when you touch rest of UIView expect collectionView items.


回答1:


You can use the following method to get the cell attributes

func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes?

and then get the frame using frame property

let attributes = self.collectionView.layoutAttributesForItem(at:indexPath)
let frame = attributes.frame

PS- you might have to convert the frame of the cell from UICollectionView to your controller's view to get its exact position.

func convert(_ rect: CGRect, 
    from view: UIView?) -> CGRect

Should do the trick




回答2:


There is a method to achieve what you want in UICollectionView delegate that you can use it to find out which cell has clicked:

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
   YourCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
   let frame = cell.layer.frame
   // here you can do what you want with frame
   // ...
}



回答3:


Basically, you need to use:

let cPoint = collectionView.layoutAttributesForItem(at indexPath: IndexPath).center

But after that you need to convert the point to your View's Coordinate system like so:

let centerPoint = collectionView.convert(center ?? CGPoint.zero, to: collectionView.superview)

Here is a full code snippet:

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    let attributes = self.collectionView?.layoutAttributesForItem(at:indexPath)
    let cPoint = attributes?.center

    /*
    this is nice. but when we scroll passed the visible items -
    we see that the point is given in the collectionView's coordinates.

    we need to convert the point to the collectionView.superview coordinate system
    */
    //solution:
    let centerPoint = collectionView.convert(cPoint, to: collectionView.superview)
}



回答4:


The indexPathsForSelectedItems returns an array of index paths (since there might be several items selected) so you need to use:

let indexPaths : NSArray = self.collectionView!.indexPathsForSelectedItems()
let indexPath : NSIndexPath = indexPaths[0] as NSIndexPath

(You should probably test to see whether several items are selected, and handle accordingly).




回答5:


Thanks all you for your good response and i found solution for same.

let cell = collectionView.cellForItem(at: indexPath) as! MyCollectionViewCell
let tap = CGPoint(x: 0, y: 0)
let convertedTap = cell.convert(tap, to: self.view)

When you will print convertedTap you will get exact position of collection item cell with respect to view.




回答6:


its very easy . In swift the position is called as indexpath. when you select a particular cell in collectionview its position is identified by index path

//initialise did select function in your view controller

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
     print("You selected cell #\(indexPath.item)!")

}

//note you can get indexpath in your "cellforItematIndexpath" function



来源:https://stackoverflow.com/questions/54529534/how-to-get-position-of-selected-item-of-collection-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!