【问题标题】:Not Detecting Tap on image view未检测到点击图像视图
【发布时间】:2011-10-17 04:19:40
【问题描述】:

我有一个应用程序,我在图像视图中从 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”

这段代码有什么错误,所以它会显示这种类型的消息。

【问题讨论】:

  • 试试这个:- [self.view sendsubviewtoback : yourscrollView]; [你的滚动视图将子视图带到前面:图像视图];可能b你的scrollView出现在你的Imageview前面所以它不会触摸......在删除scrollview后尝试tp点击一次,这样你可能会清楚它是否与ScrollView有关。
  • 当你没有调用你的线程并且没有初始化自动释放池......并在你的函数完成时释放它时出现此消息..

标签: iphone xcode uiscrollview uiimageview uigesturerecognizer


【解决方案1】:

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

【讨论】:

  • 我确实启用了它,但我仍然遇到这个问题。
【解决方案2】:

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

//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];

希望这会有所帮助。

【讨论】:

  • 它不工作。它显示此消息 2011-08-01 10:01:46.999 ImageScroll[443:1907] * __NSAutoreleaseNoPool(): 类 UIView 的对象 0x4e412d0 自动释放,没有适当的池 - 只是泄漏
  • 检查一下。我已经编辑了我的答案。请检查//EDIT:1//EDIT:2下面的行
  • 我没有在控制台上显示任何消息,但它没有执行。当我上传这个视图然后终止程序而不执行任何操作
  • @ram:尝试调试并查看它实际崩溃的位置
  • 在我的代码中也没有检测到。当我点击图像然后没有发生任何事件。
【解决方案3】:

试试这个

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

【讨论】:

    猜你喜欢
    • 2013-07-17
    • 1970-01-01
    • 1970-01-01
    • 2016-11-08
    • 2018-08-28
    • 1970-01-01
    • 2017-01-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多