在UIImageView上检测触摸事件

3
我在UIView上放置了4个UIImageView,我给它们命名了。
UIImageView myImg1 = [[UIImageView alloc] init];     
UIImageView myImg2 = [[UIImageView alloc] init];     
UIImageView myImg3 = [[UIImageView alloc] init]; 
UIImageView myImg4 = [[UIImageView alloc] init];

我该如何告诉编译器我刚刚触摸了UIImageView 1或UIImageView 2。如果我只能使用UIImageView来完成这个任务,而不能使用按钮,请指导我解决这个问题。谢谢。

4个回答

6
一种时尚的解决方案是使用 UIGestureRecognizer 对象:
在你的 loadView 方法中(或者在你编写 UIImageView 的绘制代码的任何地方):
   UITapGestureRecognizer *tapRecognizer;
    UIImageView *anImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 100, 100)];
    [anImageView setTag:0]; //Pay attention here!, Tag is used to distinguish between your UIImageView on the selector
    [anImageView setBackgroundColor:[UIColor redColor]];
    [anImageView setUserInteractionEnabled:TRUE];
    tapRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageViewDidTapped:)] autorelease];
    tapRecognizer.numberOfTapsRequired = 1;
    [anImageView addGestureRecognizer:tapRecognizer];
    [contentView addSubview:anImageView];
    [anImageView release];

    UIImageView *anotherImageView = [[UIImageView alloc] initWithFrame:CGRectMake(80, 180, 100, 100)];
    [anotherImageView setTag:1]; //Pay attention here!, Tag is used to distinguish between your UIImageView on the selector
    [anotherImageView setBackgroundColor:[UIColor greenColor]];
    [anotherImageView setUserInteractionEnabled:TRUE];
    tapRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageViewDidTapped:)] autorelease];
    tapRecognizer.numberOfTapsRequired = 1;
    [anotherImageView addGestureRecognizer:tapRecognizer];
    [contentView addSubview:anotherImageView];
    [anotherImageView release];

这是一个简单的 imageViewDidTapped: 示例方法,你可以使用它来区分上面的两个 UIImageView 对象:
- (void)imageViewDidTapped:(UIGestureRecognizer *)aGesture {
    UITapGestureRecognizer *tapGesture = (UITapGestureRecognizer *)aGesture;

    UIImageView *tappedImageView = (UIImageView *)[tapGesture view];

    switch (tappedImageView.tag) {
        case 0:
            NSLog(@"UIImageView 1 was tapped");
            break;
        case 1:
            NSLog(@"UIImageView 2 was tapped");
            break;
        default:
            break;
    }
}

1
你应该考虑使用tag属性来实现这个功能。给每张图片设置一个唯一的tag,然后利用这些信息来确定哪张图片被点击了。例如,通过设置标签:
UIImageView myImg1 = [[UIImageView alloc] init];     
myImg1.tag = 1;

当你接收到对 img 的触摸时,请调用

img.tag

获取标签。


1
UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
        singleFingerTap.numberOfTapsRequired = 1;
        [myImg1 addGestureRecognizer:singleFingerTap];
        [myImg2 addGestureRecognizer:singleFingerTap];
        [singleFingerTap release];

动作方法

-(void)handleSingleTap:(UIGestureRecognizer*)sender{
    //your code here
}

1
但问题仍未得到解答,这将识别触摸但不是所触摸的确切图像,此代码需要进行一些修改。 - rishi
嗯。让我们读一下问题 - _我该如何告诉编译器我刚刚触摸了UIImageView 1或UIImageView 2_。在我的答案中,OP从UIImageView 1 UIImageView 2获取触摸。 - beryllium
这就是为什么我们可以在图像上使用标签,并基于这些标签来识别,还有其他几种方法,比如我可以拥有ImageView的坐标等等... - rishi
手势识别器一次只能添加到一个视图中。 - user467105

1

你可以像这样做。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

        UITouch *touch = [touches anyObject];

       CGPoint touchLocation = [touch locationInView:self.view];
   if(CGRectContainsPoint(myImg1.view.frame,touchLocation))
 {
NSLog(@" Image 1");
}
else if(CGRectCont..

注意:如果该点位于多个图像视图中,则会输出NSLog。 在这种情况下,请检查哪个图像视图在顶部。


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