未检测到点击图像视图

时间:2011-08-01 04:41:02

标签: iphone xcode uiscrollview uiimageview uigesturerecognizer

我有一个应用程序,我在图像视图中从URL调用图像。图像视图被添加为滚动视图的子视图。在我的实现文件中,我使用了这段代码

    - (void)loadView {
    [super loadView];


        // add gesture recognizers to the image view
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
    UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
    UITapGestureRecognizer *twoFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTwoFingerTap:)];

    [doubleTap setNumberOfTapsRequired:2];
    [twoFingerTap setNumberOfTouchesRequired:2];


    NSURL *imgUrl=[[NSURL alloc] initWithString:@"http://www.deviantart.com/download/80153093/Sexy_Kabuto_by_dark_tarou.jpg"];                 
    NSData *imgData = [NSData dataWithContentsOfURL:imgUrl];
    UIImage *img = [UIImage imageWithData:imgData];
    imageView = [[UIImageView alloc] initWithImage:img];



    // set the tag for the image view
    [imageView setTag:ZOOM_VIEW_TAG];



    [imageView addGestureRecognizer:singleTap];
    [imageView addGestureRecognizer:doubleTap];
    [imageView addGestureRecognizer:twoFingerTap];

    [singleTap release];
    [doubleTap release];
    [twoFingerTap release];

    [self.imageScrollView addSubview:imageView];
    [imgUrl release]; 

    // calculate minimum scale to perfectly fit image width, and begin at that scale
    float minimumScale = [imageScrollView frame].size.width  / [imageView frame].size.width;
    [imageScrollView setMinimumZoomScale:minimumScale];
    [imageScrollView setZoomScale:minimumScale];
    NSLog(@"%d",imageView.tag);
}

当我在模拟器上运行时,它没有检测到点击识别器。在控制台窗口中显示此消息

  

2011-08-01 10:01:46.999 ImageScroll [443:1907] * __NSAutoreleaseNoPool():类UIView的对象0x4e412d0自动释放,没有池到位 - 只是泄漏   无法访问变量“doubleTap”   “   无法访问变量“singleTap”   无法访问变量“doubleTap”   无法访问变量“doubleTap”

此代码中的错误是什么,以便显示此类消息。

3 个答案:

答案 0 :(得分:3)

您检查过“UserInteractionEnabled”吗?我相信它默认是NO。 (在UIView中,默认值为YES)

答案 1 :(得分:0)

尝试自动释放而不是像这样立即释放:

//EDIT:1
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
UITapGestureRecognizer *singleTap = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)] autorelease];
UITapGestureRecognizer *doubleTap = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)] autorelease];
UITapGestureRecognizer *twoFingerTap = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTwoFingerTap:)] autorelease];
//EDIT:2
[pool release];

然后注释掉

[singleTap release];
[doubleTap release];
[twoFingerTap release];

希望这会有所帮助。

答案 2 :(得分:0)

试试这个

    [self.view addGestureRecognizer:singleTap];
    [self.view addGestureRecognizer:doubleTap];
    [self.view addGestureRecognizer:twoFingerTap];