iPhone - 如何从自定义单元格中捕获uiimageview的触摸事件

3
我希望创建一个自定义的表格单元,并在其中放置一个imageView。当用户点击imageView时,弹出一个视图在viewController上,并且该视图将知道哪个单元格中的图片被按下。
谢谢。
4个回答

11

使用 UITapGestureRecognizer 手势来检测任何继承自 UIView 的视图的触摸。

按以下方式使用.....

将轻击手势 UITapGestureRecognizer 添加到 myImageView 视图(类型为 UIImageView)中。

UITapGestureRecognizer *myTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gestureTapEvent:)];
myImageView.userInteractionEnabled = YES;
[myImageView addGestureRecognizer:myTapGesture];
[myTapGesture release];    

实现gestureTapEvent:方法以接收触摸事件。

-(void)gestureTapEvent:(UITapGestureRecognizer *)gesture 
{

}

更新:

你可以通过访问UIGestureRecognizerview属性来访问与手势相关联的视图。

@property(nonatomic, readonly) UIView *view;

你的`gestureTapEvent:`方法应该如下所示。

-(void)gestureTapEvent:(UITapGestureRecognizer *)gesture 
{
     UIImageView* myImageView = (UIImageView*)gesture.view ;
}

但是我怎么知道在点击方法中哪个 ImageView 被点击了呢? - yosifz8

2

你有多种可能性:

  • 使用UIGestureRecognizer在imageView上添加UITapGestureRecognizer(我推荐这个解决方案)
  • 子类化UIImageView,覆盖touchesBegan:withEvent:方法等
  • 在UIImageView上添加一个按钮(类型为Custom,以避免绘制按钮的框架)

例如:

UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageViewClicked:)];
[cell.yourImageView addGestureRecognizer:gestureRecognizer];
[gestureRecognizer release];

[编辑]

然后,当您实现imageViewClicked方法时,您可以使用GestureRecognizer的view属性获取被点击的ImageView。从那里开始,您可以:

  • 使用您的imageView的标签(如果您在tableView:cellForRowAtIndexPath:方法中设置了它),以检索标签并根据所设置的内容执行任何操作(例如,您可能已经在tableView:cellForRowAtIndexPath:中设置了imageView.tag = indexPath.row,然后再获取该indexPath行)
  • 遍历imageView的superviews直到UITableViewCell,然后请求其indexPath以获取它并进行任何您想要的操作。

示例:

-(void)imageViewClicked:(UITapGestureRecognizer*)gestRecognizer
{
    UIImageView* iv = (UIImageView*)gestRecognizer.view;
    NSInteger tag = iv.tag; // then do what you want with this

    // or get the cell by going up thru the superviews until you find it
    UIView* cellView = iv;
    while(cellView && ![cellView isKindOfClass:[UITableViewCell class]])
        cellView = cellView.superview; // go up until you find a cell

    // Then get its indexPath
    UITableViewCell* cell = (UITableViewCell*)cellView;
    NSIndexPath* indexPath = [self.tableView indexPathForCell:cell];
}

但是我如何知道在点击方法中点击了哪个ImageView? - yosifz8
你必须跟踪indexPath.row。 - Maulik
1
UIGestureRecognizer的“view”属性(作为参数传递给你的“imageViewClicked:”选择器)将使您访问UIImageView。然后,例如,您可以使用imageView的标记来跟踪与其关联的哪个模型对象或indexPath(如果在返回单元格时设置了标记),或获取父UITableViewCell并询问其indexPath。(我会更新我的答案以提供代码) - AliSoftware

0

你可以使用自定义的 UIButton 代替 UIImageView,并将相同的图像分配给按钮,然后你就能够获取该按钮的 touchUpInside 事件。只需将 button.tag = indexpath.row 分配给按钮,这样你就能知道哪个单元格的按钮被按下。

-(IBAction)ButtonPressed:(id)sender{
     UIButton *btnSelected = sender;
     switch(btnSelected.tag){
       case: 0
       ///Cell 0's Button Pressed. 
       break;
       ......
       ......
}

希望这有所帮助。

0

要为UIImageView添加触摸事件,请在.m文件中使用以下格式

UITapGestureRecognizer *newTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(newTapMethod)];

[_imageButtonName setUserInteractionEnabled:YES];

[_imageButtonName addGestureRecognizer:newTap];

然后创建新的tap方法(或者你想起什么名字都可以)

-(void)newTapMethod{
//...
}

希望这能帮到你。


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