检测在uiscrollView中挖掘哪个UIImage

时间:2014-10-14 12:19:56

标签: 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)];

有什么想法吗?

3 个答案:

答案 0 :(得分:0)

您可以使用-hitTest:withEvent:来获取所需的视图,例如,如果您将_UumbapGestureRecognizer添加到_thumbnailScroll,则可以这样做:

// Add gesture recognizer to a superview of imageviews (i.e. _thumbnailScroll)
UITapGestureRecognizer *singleTapGR = [[UITapGestureRecognizer alloc] initWithTarget:self    action:@selector(onSingleTapGestureRecognized:)];
[_thumbnailScroll addGestureRecogniser:singleTapGR];
...
// Implement it on your controller (who self was above)
- (void)onSingleTapGestureRecognized:(UITapGestureRecognizer *)singleTapGestureRecognizer
{
    UIView *tappedView = [_thumbnailScroll hitTest:[singleTapGestureRecognizer locationInView: _thumbnailScroll] withEvent:nil];

    // check if tappedView is one of your UIImageViews.
    // tapped view will be one of your _thumbnailScroll subviews or itself
    // so you use just use this information later
}

如果您只需要知道哪一个被轻击,那么为每个UIImageView添加一个手势识别器是多余的。

答案 1 :(得分:0)

将此用于循环内部的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给出的行动方法。

答案 2 :(得分:0)

有几种方法可以做到这一点 。其中一种方法是在每个UIImageView上添加UITapGestureRecognizer。为UITapGestureRecognizer设置一个选择器,如

UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageTapped:)];

[imageView addGestureRecognizer:tap];

and in imageTapped: mathod

-(void)imageTapped:(UITapGestureRecognizer*)recognizer{
    UIImageView *imgView=(UIImageView*)[recognizer view];   

}

不要忘记为UIImageView启用用户交互。因为它默认是禁用的