ios 如何获取位于 UICollectionView 中心的单元格的 indexPath
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19265655/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to get indexPath for cell which is located in the center of UICollectionView
提问by Sasha Prent
How can i find indexPath
for cell
in the middle of UICollectionView
?
我如何才能找到indexPath
用于cell
在中间UICollectionView
?
I have horizontal scrolling and only one big cell
is visible (partially two other cell
s on the sides are visible as well).
I need to delete cell
located in the center (means - current cell
) without touching it.
我有水平滚动,只有一个cell
是可见的(cell
侧面的另外两个部分也可见)。我需要删除cell
位于中心(意味着 - 当前cell
)而不触摸它。
Only pressing "Trash" button and confirm Delete. But now it delete only first cell
s.
只需按“垃圾箱”按钮并确认删除。但现在它只删除 first cell
s。
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex == actionSheet.destructiveButtonIndex) {
initialPinchPoint = ????????
self.tappedCellPath = [self.collectionView indexPathForItemAtPoint:initialPinchPoint];
Technique *technique = [arrayOfTechnique objectAtIndex:self.tappedCellPath.row];
[self deleteData:[NSString stringWithFormat:@"DELETE FROM TECHNIQUES WHERE TECHNIQUENAME IS '%s'",[technique.techniquename UTF8String]]];
[arrayOfTechnique removeObjectAtIndex:self.tappedCellPath.row];
//[arrayOfTechnique removeObjectAtIndex:[custom_layout ]];
[self removeImage:technique.techniquename];
[self.collectionView performBatchUpdates:^{
[self.collectionView deleteItemsAtIndexPaths:[NSArrayarrayWithObject:self.tappedCellPath]];
} completion:nil];
[self checkArrayCount];
}
}
采纳答案by micantox
Like you did yourself, indexPathForItemAtPoint
is a good way of finding the index path of the element you're interested in. If your question is: how do i know the coordinates of this point? Then you should try with (using the same name you gave it in your code snippet):
就像你自己做的那样,indexPathForItemAtPoint
是找到你感兴趣的元素的索引路径的好方法。如果你的问题是:我怎么知道这个点的坐标?然后您应该尝试使用(使用您在代码片段中为其指定的名称):
initialPinchPoint = CGPointMake(self.collectionView.center.x + self.collectionView.contentOffset.x,
self.collectionView.center.y + self.collectionView.contentOffset.y);
回答by Jlam
This would be better code because it's cleaner and easier to read, all the content offset calculation is superfluous:
这将是更好的代码,因为它更清晰、更易于阅读,所有内容偏移计算都是多余的:
NSIndexPath *centerCellIndexPath =
[self.collectionView indexPathForItemAtPoint:
[self.view convertPoint:[self.view center] toView:self.collectionView]];
This would also be the correct representation of what you're actually trying to do:
1. Taking the center point of your viewcontroller's view - aka visual center point
2. convert it to the coordinate space of the view you're interested in - which is the collection view
3. Get the indexpath that exist at the location given.
这也是您实际尝试执行的操作的正确表示:
1. 取视图控制器视图的中心点 - 又名视觉中心点
2. 将其转换为您感兴趣的视图的坐标空间 - 其中是集合视图
3. 获取存在于给定位置的索引路径。
回答by MCR
Here's what I did in Swift 3
这是我在 Swift 3 中所做的
private func findCenterIndex() {
let center = self.view.convert(self.collectionView.center, to: self.collectionView)
let index = collectionView!.indexPathForItem(at: center)
print(index ?? "index not found")
}
回答by Bobj-C
Swift:
迅速:
extension UICollectionView {
var centerPoint : CGPoint {
get {
return CGPoint(x: self.center.x + self.contentOffset.x, y: self.center.y + self.contentOffset.y);
}
}
var centerCellIndexPath: IndexPath? {
if let centerIndexPath: IndexPath = self.indexPathForItemAtPoint(self.centerPoint) {
return centerIndexPath
}
return nil
}
}
Usage :
用法 :
if let centerCellIndexPath: IndexPath = collectionView.centerCellIndexPath {
print(centerCellIndexPath)
}
Swift 3:
斯威夫特 3:
extension UICollectionView {
var centerPoint : CGPoint {
get {
return CGPoint(x: self.center.x + self.contentOffset.x, y: self.center.y + self.contentOffset.y);
}
}
var centerCellIndexPath: IndexPath? {
if let centerIndexPath = self.indexPathForItem(at: self.centerPoint) {
return centerIndexPath
}
return nil
}
}
回答by DarkoM
Thank you micantox!
谢谢你!
I had multiple visible cells of UICollectionView and needed to position the cell at the centre of the collection view. Something similar to Adobe Content Viewer. If someone is struggling with similar scenario:
我有 UICollectionView 的多个可见单元格,需要将单元格定位在集合视图的中心。类似于 Adobe Content Viewer 的东西。如果有人在类似情况下苦苦挣扎:
#pragma mark - UIScrollViewDelegate methods
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
CGPoint centerPoint = CGPointMake(self.pageContentCollectionView.center.x + self.pageContentCollectionView.contentOffset.x,
self.pageContentCollectionView.center.y + self.pageContentCollectionView.contentOffset.y);
NSIndexPath *centerCellIndexPath = [self.pageContentCollectionView indexPathForItemAtPoint:centerPoint];
[self.pageContentCollectionView scrollToItemAtIndexPath:centerCellIndexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
}
回答by Alexander Khitev
I made like for horizontal UICollectionView I use Swift 2.x.
我喜欢水平 UICollectionView 我使用 Swift 2.x。
private func findCenterIndex() {
let collectionOrigin = collectionView!.bounds.origin
let collectionWidth = collectionView!.bounds.width
var centerPoint: CGPoint!
var newX: CGFloat!
if collectionOrigin.x > 0 {
newX = collectionOrigin.x + collectionWidth / 2
centerPoint = CGPoint(x: newX, y: collectionOrigin.y)
} else {
newX = collectionWidth / 2
centerPoint = CGPoint(x: newX, y: collectionOrigin.y)
}
let index = collectionView!.indexPathForItemAtPoint(centerPoint)
print(index)
}
override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
findCenterIndex()
}
回答by s1m0n
UICollectionView
has a method - (NSIndexPath *)indexPathForItemAtPoint:(CGPoint)point
.
This method return the index path of the item at the specified point. You could calculate the point that represents the center of the UICollectionView
and then use that CGPoint
and this method to retrieve the index path of the item you want to delete.
UICollectionView
有方法- (NSIndexPath *)indexPathForItemAtPoint:(CGPoint)point
。
此方法返回项目在指定点的索引路径。您可以计算代表 中心的点,UICollectionView
然后使用该点CGPoint
和此方法来检索要删除的项目的索引路径。
回答by Fidel López
Swift 4
斯威夫特 4
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
let indexPath = collectionView.indexPathForItem(at: collectionView.bounds.center)
}
回答by ndriqim.haxhaj
For some of you that might be experiencing some troubles on getting the center, please look at this potential solution:
对于你们中的一些人可能在获得中心时遇到一些麻烦,请查看这个潜在的解决方案:
func centerCell()->UICollectionViewCell? {
// Asuming your scrolling is horizontal
let viewHorizontalCenter = self.view.bounds.width / 2
let center = CGPoint(x: viewHorizontalCenter, y: self.collectionView.center.y)
let convertedPoint = self.view.convert(center, to: self.unitsCollectionView)
let center = CGPoint(x: self.view.bounds.width / 2, y: self.unitsCollectionView.center.y)
let convertedPoint = self.view.convert(center, to: self.unitsCollectionView)
for cell in unitsCollectionView.visibleCells {
if cell.frame.contains(convertedPoint) {
print("Hello")
return cell
}
}
return nil
}
回答by Md. Ibrahim Hassan
Based on @micantoxanswer. Updated code for Swift 5
基于@micantox 的回答。Swift 5 的更新代码
let initialPinchPoint = CGPoint(x: collectionView.center.x + collectionView.contentOffset.x,
y: collectionView.center.y + collectionView.contentOffset.y)