ios - 如何获取 self.view 中 UICollectionViewCell 的位置?

标签 ios objective-c

我使用下面的代码来获取触摸时单元格的位置。 但是,当 UICollectionView.bounds.size.width > 320,或 > 640 时,"origin.x"返回的值通常 > 320,例如 657、469。因为单元格内有一些图像.所以当我触摸第二页上的单元格时。返回的值 X 可能是 368.0 或其他值。

我只需要获取当前 View 中的值X。(320 * 480)。

UICollectionViewLayoutAttributes *attributes = [self.collectionView layoutAttributesForItemAtIndexPath:indexPath]; - (void)viewDidLoad { // attach long press gesture to collectionView UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)]; lpgr.minimumPressDuration = .5; //seconds lpgr.delegate = self; [self.collectionView addGestureRecognizer:lpgr]; } -(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer { if (gestureRecognizer.state != UIGestureRecognizerStateEnded) { return; } CGPoint p = [gestureRecognizer locationInView:self.collectionView]; NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:p]; if (indexPath == nil){ NSLog(@"couldn't find index path"); } else { // get the cell at indexPath (the one you long pressed) UICollectionViewCell* cell = [self.collectionView cellForItemAtIndexPath:indexPath]; // do stuff with the cell } }

最佳答案

如果我对你的问题理解正确,你的 Collection View 可能有多个水平页面。假设这些页面是 frame-width,你可以这样做:

int xVal = (int)p.x % (int)self.view.frame.size.width;

请记住,您将失去小数精度。

关于ios - 如何获取 self.view 中 UICollectionViewCell 的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23688099/

相关文章:

ios - Xcode 中的这个错误是什么意思?

ios - 了解objc中 block 存储管理的一种极端情况

objective-c - 如何解除多个 View Controller ?

ios - 无法将删除 subview 置于前面

ios - Swift 3 - 线程 1 : EXC_BAD_INSTRUCTION (code = EXC_I386_INVOP, 子代码 = 0x0)

ios - 我们可以在 iOS 13 中模拟 NFC 卡吗?

iphone - 来自 PDF 的高质量 UIImage

iPhone SDK : How to add an outline to UILabel's text

ios - Swift:在自定义 TableViewCell 上设置 UITextfieldDelegate

objective-c - UITextView:键入退格键时,字符计数不正确