ios - 检测在 uiscrollView 中点击了哪个 UIImage

标签 ios objective-c iphone uiscrollview uiimage

我有一个 UIScrollView装载了几个 UIImages ,对于这个例子,它加载了 5 UIImages 中的相同图片.如何检测 UIImages 中的哪一个?用户点击的?

这就是我正在使用的:

    _thumbnailScroll.delegate = self;

    [_thumbnailScroll setBackgroundColor:[UIColor blueColor]];
    [_thumbnailScroll setCanCancelContentTouches:NO];

    _thumbnailScroll.indicatorStyle = UIScrollViewIndicatorStyleWhite;
    _thumbnailScroll.clipsToBounds = NO;
    _thumbnailScroll.scrollEnabled = YES;
    _thumbnailScroll.pagingEnabled = YES;

    NSUInteger nimages = 0;
    NSInteger tot=0;
    CGFloat cx = 0;
    for (nimages = 0; nimages < 5 ; nimages++) {

        UIImage *image = [UIImage imageWithContentsOfFile:[path stringByAppendingPathComponent:@"newPics/thumb/thumb1.png" ]];
        if (tot==5) {
            break;
        }
        if (5==nimages) {
            nimages=0;
        }

        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

        CGRect rect = imageView.frame;
        rect.size.height = 150;
        rect.size.width = 150;
        rect.origin.x = cx;
        rect.origin.y = 0;

        imageView.frame = rect;


        [_thumbnailScroll addSubview:imageView];
        _thumbnailScroll.userInteractionEnabled = YES;
        // [imageView release];

        cx += imageView.frame.size.width+100;
        tot++;
    }

    //self.pageControl.numberOfPages = nimages;
    [_thumbnailScroll setContentSize:CGSizeMake(cx, [_thumbnailScroll bounds].size.height)];

有任何想法吗 ?

最佳答案

将此用于循环内的点击监听器。

 UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self    action:@selector(onSingleTapGestureRecognized:)];
 singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
 singleTap.delegate = self;
 [imageview1 addGestureRecogniser:singleTap];
 [singleTap1 release];

  imageview1.userInteractionEnabled = YES; 



由 MANIAK_dobrii 给出的 Action 方法。

关于ios - 检测在 uiscrollView 中点击了哪个 UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26360677/

相关文章:

ios - 无法设置多个 Controller 的 UILabel 字段

ios - 无法识别的选择器发送到实例到新添加的方法;给先前定义的方法的消息工作正常

ios - Facebook 应用程序请求通知未出现

iphone - init 在这个单例类中有什么用..?

iphone - 如何将 nsstring 转换为二进制并将二进制转换为十六进制

iphone - 如何制作两个像 2 行 uipickerview 一样工作的自定义表格 View

iphone - 用数据加载新 View - 不替换 NSString

ios - 如何更改图像 tintColor

objective-c - Obj-C 中的 id 和委托(delegate)用法

iphone - XML解析:如何忽略空格或输入/返回空格