获取UICollectionViewCell的触摸位置

9

我正在尝试获取所选UICollectionviewCell内的触摸位置。 我有一个UIViewController,vc,它包含一个UICollectionView,collectionView作为其视图。 在collectionView内部是UICollectionViewCells。 vc符合 UICollectionViewDelegate ,因此最好在委托回调中获取触摸位置, collectionView:(UICollectionView *)collection didSelectItemAtIndexPath:(NSIndexPath *)indexPath ,(如果可能)。 有什么建议吗? 谢谢。

+--------------------------------------------+
| UICollectionView                           |
|                                            |
|  +-------------+       +-------------+     |
|  |  UICollec   |       |             |     |
|  |  tionView   |       |             |     |
|  |  Cell       |       |     *<---------------<Touch here
|  |             |       |             |     |
|  +-------------+       +-------------+     |
|                                            |
+--------------------------------------------+    

*更新* 最终我使用了UIGestureRecognizer检测了对UIViewCollectionView的轻触,并将轻触点转换为UICollectionViewCell视图。如果有更好的方法,请告诉我。谢谢。

-(void) viewDidLoad {
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
    [tapGesture setNumberOfTapsRequired:1];
    [self.collectionView addGestureRecognizer:tapGesture];
}

-(void) handleTap:(UIGestureRecognizer*) gesture {
    if (gesture.state == UIGestureRecognizerStateEnded) {
    CGPoint tappedPoint = [gesture locationInView:_collectionView];
    NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:tappedPoint];
    CollectionViewCell *cell = (CollectionViewCell*)[self.collectionView cellForItemAtIndexPath:indexPath];
    CGPoint pointWRTCell = [cell convertPoint:tappedPoint fromView:self.collectionView];
    NSLog(@"collectionView point(%1.1f,%1.1f); cell point (%1.1f,%1.1f)",
          tappedPoint.x,tappedPoint.y,pointWRTCell.x,pointWRTCell.y);
    }
}
4个回答

6
您可能需要一个自定义单元格来实现此操作。 didSelectItemAtIndexPath 只会告诉你选中了一个单元格,但不会告诉你在单元格内的具体位置被点击了。

感谢您的回复。子类化“UICollectionView”可能会起作用。我最终使用了“UITabGestureRecognizer”,并将点击位置从“UICollectionView”转换为“UICollectionViewCell”。 - MobileDev
一些背景信息可能有助于我们提出更好的解决方案。您试图解决什么问题? - CrimsonChris
当用户在单元格的特定位置(而不是单元格本身)选项卡时,我只想删除一个单元格。 - MobileDev
啊!最简单的方法可能是创建一个自定义的UICollectionViewCell,其中包含一个按钮,并且具有一个弱引用的代理。让您的控制器成为代理。该单元格可以具有indexPath属性(每次在cellForRowAtIndexPath中设置它以避免单元格重用错误)。当按钮被按下时,请它调用代理上的一个方法(例如deleteButtonPressed:(id)sender atIndexPath:(NSIndexPath *)indexPath;)。 - CrimsonChris
刚看到你的更新。那个解决方案似乎有点脆弱,但我猜它可以避免子类化,这总是不错的。我曾经通过在运行时添加我的按钮并将索引路径作为UITableViewCell的关联属性附加来完成类似的事情,而无需子类化。 - CrimsonChris

1

UIView有一个方法func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView?。您可以在集合视图或集合视图单元格中重写此方法以获取触摸点。

还有一些方法,例如func convertPoint(_ point: CGPoint, toView view: UIView?) -> CGPoint,可用于将不同坐标系中的点转换。


1
获取indexPath以尝试此代码...
(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CellId" forIndexPath:[indexPath row]]; 

[[cell myButton] addTarget:self action:@selector(myClickEvent:event:) forControlEvents:UIControlEventTouchUpInside];

return cell;

}


(IBAction)myClickEvent:(id)sender event:(id)event {

NSSet *touches = [event allTouches];

UITouch *touch = [touches anyObject];

CGPoint currentTouchPosition = [touch locationInView:_myCollectionArray];

NSIndexPath *indexPath = [_myCollectionArray indexPathForItemAtPoint: currentTouchPosition];

}

0
你应该为保存UICollectionViewCell中的触摸位置添加属性。
@interface YourCollectionViewCell : UICollectionViewCell
@property (nonatomic, assign) CGPoint       touchPoint;
@end

然后在 YourCollectionViewCell.m 中添加 touchesBegan 事件

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    self.touchPoint = [[touches anyObject] locationInView:self];
}

最后,您可以在UICollectionView中使用touchPoint

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    YourCollectionViewCell * cell = [collectionView cellForItemAtIndexPath:indexPath];
    NSLog(@"touch location %f, %f", cell.touchPoint.x, cell.touchPoint.y);
}

我认为这是获取触摸位置的更简单的方法。我在我的代码中使用它。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接