Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/112.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/44.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios uiscrollview内部uiimageview上的UIAPTgestureRecognitor_Ios_Iphone_Objective C_Uiscrollview_Uiimageview - Fatal编程技术网

Ios uiscrollview内部uiimageview上的UIAPTgestureRecognitor

Ios uiscrollview内部uiimageview上的UIAPTgestureRecognitor,ios,iphone,objective-c,uiscrollview,uiimageview,Ios,Iphone,Objective C,Uiscrollview,Uiimageview,我遇到了一个问题,我制作的点击手势识别器只对滚动视图中的最后一项起作用 我有一个图像数组,这些图像被添加到UIImageView,UIImageView被添加到ScrollView,代码如下: UIImageView *imageView1; for (int i = 0; i < numberOfViews; i++) { xOrigin = i * imageSize; imageView1 = [[UIImageView alloc] initWithFrame:CG

我遇到了一个问题,我制作的点击手势识别器只对滚动视图中的最后一项起作用

我有一个图像数组,这些图像被添加到UIImageView,UIImageView被添加到ScrollView,代码如下:

UIImageView *imageView1;
for (int i = 0; i < numberOfViews; i++) {
    xOrigin = i * imageSize;
    imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin,50,100,50)];
    [imageView1 setUserInteractionEnabled:YES];
    [imageView1 addGestureRecognizer:tap]; // The gesture I want
    [imageView1 setImage:[images objectAtIndex:i]];
    [scrollView addSubview:imageView1];
}
// Set the contentSize equal to the size of the UIImageView
// scrollView.contentSize = imageView.scrollview.size;
scrollView.contentSize = CGSizeMake(numberOfViews * imageSize, imageSize);


// Finally, add the UIScrollView to the controller's view
[self.view addSubview:scrollView];
UIImageView*imageView1;
对于(int i=0;i
上述代码可以正确地将图像按顺序添加到滚动视图中,以及将点击添加到列表中的最后一项中。也就是说,他们中的其他人没有得到点击动作


提前谢谢

这是有意义的,因为您在所有图像视图中添加了一个手势识别器。将相同的手势识别器添加到第二个视图时,它将从上一个视图中删除。您应该向scrollview添加一个手势识别器,然后根据触摸位置确定点击了哪个图像视图,或者向每个图像视图添加单独的手势识别器

UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
UIGestureRecognizer *tapGestureRecongnizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[scrollView addGestureRecognizer:tapGestureRecongnizer];


- (void)handleTap:(UIGestureRecognizer *)tapGestureRecognizer
{
    CGPoint location = [tapGestureRecognizer locationInView:self.scrollView];
    for (UIImageView *imageView in self.imageViews) {
        if (CGRectContainsPoint(imageView.frame, location)) {
            // here is the imageView being tapped
        }
    }
}

另一种方法是使用
ui按钮
s而不是
ui图像视图
s,然后您还可以免费获得“突出显示效果”。

正如我所看到的,您只有一个点击手势对象,这就是为什么当您运行循环时,它只会添加到最后一个图像视图中

您需要为一个图像视图创建一个点击手势对象

UIImageView *imageView1;
for (int i = 0; i < numberOfViews; i++) {
    xOrigin = i * imageSize;
    imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin,50,100,50)];
    [imageView1 setUserInteractionEnabled:YES];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapMethod:)];
    [imageView1 addGestureRecognizer:tap]; // The gesture I want
    [imageView1 setImage:[images objectAtIndex:i]];
    [scrollView addSubview:imageView1];
}
// Set the contentSize equal to the size of the UIImageView
// scrollView.contentSize = imageView.scrollview.size;
scrollView.contentSize = CGSizeMake(numberOfViews * imageSize, imageSize);
// Finally, add the UIScrollView to the controller's view
[self.view addSubview:scrollView];
UIImageView*imageView1;
对于(int i=0;i
我如何解决这个问题?您好,如果我的回答对您有所帮助,请将其标记为已接受。感谢这个问题在另一个问题上帮助了我-记住在UIImageView上设置UserInteractionEnabled!