0

我正在创建自定义 UIImageViews 并放置在 UIScrollView 中的 UIImageView 上。当用户点击自定义 UIImageView 时,它会显示一个弹出框。

我可能遇到的问题是两个自定义 UIImageViews 是否重叠。我需要问用户他想要哪一个。

我怎么知道哪些自定义 UIImageViews 在一个水龙头内?如果检测到点击,我需要每个视图自行返回。如果返回的视图不止一个,那么我可以询问用户他想要哪一个。

每个自定义 UIImageView 都有一个 UITapGestureRecognizer 创建:

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(select)];
singleTap.numberOfTapsRequired = 1;
singleTap.delegate = self;

[self addGestureRecognizer:singleTap];

现在,只有最顶部的自定义 UIImageView 正在获取点击并显示弹出框。

4

2 回答 2

2

我不确定你打算如何识别哪个图像是哪个,但在这个例子中我使用了标签。以下将接收滚动视图中的触摸位置,并将该点与滚动视图子视图中的图像视图帧进行比较。然后它将匹配到可变数组的图像的标签添加。

注意:如果您在关闭警报时不清空此数组,则将不断向其中添加新对象。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView:myScrollView];

    for (UIImageView *myImageView in myScrollView.subviews) {
        if (CGRectContainsPoint(myImageView.frame, location)) {
            [someMutableArray addObject:[NSNumber numberWithInteger:myImageView.tag]];
        }
    }
}
于 2012-09-11T20:29:19.490 回答
1

I assume by your question that the views are transparent so the user can see that in fact there is overlap, and may intentionally tap the area of overlap.

In any case what you need to do in this case is get the location of the tap:

[tapGesture locationInView:scrollView]

Then walk the scrollView's subView array, getting each of your UIImageView's, getting its frame, and seeing if the tap is inside that frame.

Now you have an array of possible images - you can pop an action sheet (whatever) and ask the user which to show.

于 2012-09-11T20:31:58.660 回答