检测包含'n'UIImageViews的UIScrollView上的单击

CF_*_*ner 6 iphone cocoa-touch tap uiscrollview uiimageview

我只是试图从UIScrollView获取被点击的UIImageView.

我在网上找到了两种实现上述目的的方法.

第一种方法:在将其添加到scrollviewer之前,在uiimageview上创建一个点击手势.

这种方法对我没用.handleSingleTap方法永远不会被调用.

我不知道我做错了什么/为什么这不起作用.

    UITapGestureRecognizer *singleTap = 
    [[UITapGestureRecognizer alloc] initWithTarget:self 
    action:@selector(handleSingleTap:)];
        singleTap.numberOfTapsRequired = 1;
        [imageView addGestureRecognizer:singleTap];
        [singleTap release];

        [framesSourceScrollview addSubview:imageView];
        [imageView release];

            - (void)handleSingleTap:(UIGestureRecognizer *)sender
            {   
                NSLog(@"image tapped!!!");
            }
Run Code Online (Sandbox Code Playgroud)

第二种方法:子类UIScrollView

@interface SingleTapScrollViewer : UIScrollView {
}
@end

@implementation SingleTapScrollViewer

- (id)initWithFrame:(CGRect)frame 
{
    return [super initWithFrame:frame];
}

- (void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event 
{   
    // If not dragging, send event to next responder
    if (!self.dragging) 
        [self.nextResponder touchesEnded: touches withEvent:event]; 
    else
        [super touchesEnded: touches withEvent: event];
}
@end

In the ViewController

- (void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event 
{
    // Process the single tap here
    NSLog(@"Scroll view single tapped. touches count : %d", touches.count);
UITouch *touch = [touches anyObject]; 
    UIImageView *imgView = (UIImageView*)touch.view;
    NSLog(@"tag is %@", imgView.tag);
}
Run Code Online (Sandbox Code Playgroud)

使用这个方法,touchesDown响应器会被调用,但'[touches anyobject]'不是被点击的UIImageView.

我尝试在每个UIImageView上设置'tag',我正在使用增加的计数器添加到scrollview,但无论我点击哪个imageview,我都会返回0.

我一般都是cocoa的新手,我不知道如何以任何其他方式利用这个响应者.

任何建议/提示都会很棒.

提前致谢.

Jas*_*gun 12

你在UIImageView上设置了userInteractionEnabled = YES吗?它默认关闭.