LittlePeculiar
LittlePeculiar

Reputation: 403

Adding long press gesture to UIImageView inside Scrollview

I've been researching this for a couple of hours now and nothing seems to work. I have a UIScrollView as an IBOutlet and am adding two UIImageViews via code. One is a background image and the 2nd is a photo. Everything works fine here. I am trying to add a long press gesture to the second photo but it doesn't seem to be working. Here is my code. I hope someone can see my problem.

    // loop thru images array

    UIImage *bgFrame = [UIImage imageNamed:@"photo-frame.png"];
    UIImageView *bgImageView = [[UIImageView alloc] initWithImage:bgFrame];
    bgImageView.frame = CGRectMake(posX, posY, 100, 100);
    [self.scroll addSubview:bgImageView];

    NSString *photoName = [self.photoArray objectAtIndex:i];
    UIImage *photo = [self.utils getClientPhoto:photoName];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:photo];
    imageView.userInteractionEnabled = YES;
    imageView.frame = CGRectMake(6, 9, 89, 71);
    imageView.tag = i;
    [bgImageView addSubview:imageView];

    // add long press for deletion
    UILongPressGestureRecognizer *lPressed = [[UILongPressGestureRecognizer alloc]
                                              initWithTarget:self action:@selector(imageLPressed:)];
    lPressed.delegate = self;
    lPressed.minimumPressDuration = 0.4;
    [imageView addGestureRecognizer:lPressed];

Upvotes: 0

Views: 1568

Answers (2)

Dmitry Vorobyov
Dmitry Vorobyov

Reputation: 149

imageView.userInteractionEnabled = YES; not needed, it always work fine. Setting delegate to gestureRecognizer is optional too. First of all try to add imageView to self.scroll subviews not to bgImageView. After that check your scrollView content size, if your image is out of dat size it can be visible but not available. Try to replace imageview with uibutton and check for it's uitouchupinside event.

Upvotes: 1

devluv
devluv

Reputation: 2017

Enable UserInteraction on bgImageView andimageView.

Upvotes: 1

Related Questions