简体   繁体   中英

UIPanGestureRecognizer for UIImageView inside UIImageView inside UIScrollView

Here's the idea of what I'm trying to accomplish, as if this were layers

(TOP) - Annotation Pin (30 x 30px) This is a UIImageView. - UIViewImage with a picture - UIScrollView

The idea is that you can push a button in the view a new pin image shows up on the center of the screen, but you want to reposition that pin to where it makes sense to show an area of the picture. Also, additional pins will then be placed on the center of the screen if you push the pin button again, leaving the first pin where you moved it to already.

I can add the UIPanGestureRecognizer to a small app with just a UIViewImage, but once this view is inside a view and inside a uiscrollview, it doesn't work.

Here's my declaration file, being only a delegate for the UIScrollView:

#import <UIKit/UIKit.h>

@interface ImageAnnotationVC : UIViewController <UIScrollViewDelegate>
{
    UIPanGestureRecognizer *panRecognizer;
}

Here's how my scrollView is getting initialized:

- (void)viewDidLoad
{
    [super viewDidLoad];

    imageView_image.image = originalImage;
    scrollView.contentSize = originalImage.size;
    scrollView.delegate = self;
    scrollView.minimumZoomScale = 1.0;
    scrollView.maximumZoomScale = 100.0;    
}

Here's how the pin gets created and brought up to the screen:

- (IBAction)button_addAnnotation:(id)sender
{
    UIImageView *annotationImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"push-pin-mini@3x.png"]];
    [annotationImage setCenter:CGPointMake(imageView_image.bounds.size.width / 2, imageView_image.bounds.size.height / 2)];

    panRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePan:)];
    [annotationImage addGestureRecognizer:panRecognizer];

    [imageView_image addSubview:annotationImage];
}

And here's the target method:

- (void)handlePan:(UIPanGestureRecognizer *)recognizer
{
    NSLog("In handlePan");
}

The problem is that handlePan never gets called. Any ideas? Thank you!

UIImageView默认情况下未启用用户交互,因此您必须同时对针UIImageView及其超级视图UIImageView启用用户交互,以便识别手势。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM