Not Detecting Tap on image view

2019-08-17 03:16发布

I have an application in which i am calling image from URL in image view. Image view is add as subview of scroll view. In my implementation file i have use this code

    - (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);
}

When i run on simulator then it did not detect tap recognizer. In console window it show this message

2011-08-01 10:01:46.999 ImageScroll[443:1907] * __NSAutoreleaseNoPool(): Object 0x4e412d0 of class UIView autoreleased with no pool in place - just leaking Unable to access variable "doubleTap" " Unable to access variable "singleTap" Unable to access variable "doubleTap" Unable to access variable "doubleTap"

What is error in this code so that it show this type of message.

3条回答
够拽才男人
2楼-- · 2019-08-17 03:23

Try autoreleasing instead of immediately releasing like this:

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

and then comment out the lines

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

Hope this helps.

查看更多
三岁会撩人
3楼-- · 2019-08-17 03:30

Have you checked "UserInteractionEnabled"? I believe it is NO by default. (In UIView the default value is YES)

查看更多
三岁会撩人
4楼-- · 2019-08-17 03:34

Try this

    [self.view addGestureRecognizer:singleTap];
    [self.view addGestureRecognizer:doubleTap];
    [self.view addGestureRecognizer:twoFingerTap];
查看更多
登录 后发表回答