我有一个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)];

有任何想法吗 ?

最佳答案

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

 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给出的动作方法。

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

10-09 15:35