在Scrollview中向UIImageView添加长按手势

时间:2014-09-08 02:49:40

标签: ios uiscrollview uiimageview uigesturerecognizer

我现在已经研究了几个小时,但似乎没有任何效果。我有一个UIScrollView作为IBOutlet,并通过代码添加两个UIImageViews。一个是背景图像,第二个是照片。这里一切都很好。我试图在第二张照片上添加一个长按手势,但它似乎没有工作。这是我的代码。我希望有人能看到我的问题。

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

2 个答案:

答案 0 :(得分:1)

UserInteractionbgImageView上启用imageView

答案 1 :(得分:1)

不需要

imageView.userInteractionEnabled = YES;,它总能正常工作。将delegate设置为gestureRecognizer也是可选的。首先尝试将imageView添加到self.scroll子视图而不是bgImageView。之后检查您的scrollView内容大小,如果您的图像超出dat大小,它可以显示但不可用。尝试用uibutton替换imageview并检查它的uitouchupinside事件。