320,-6ren">
gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 02:57:48 24 4
gpt4 key购买 nike

我使用下面的代码来获取触摸时单元格的位置。但是,当 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com