用于UICollectionView Objective-c的长按水龙头手势识别器

时间:2016-07-07 10:02:38

标签: ios uicollectionview uilongpressgesturerecogni

我在我的ViewController中使用UICollectionView来显示图像,我希望用户应该能够长按删除照片,但我无法检测长按手势。我已经阅读了之前的所有讨论,并尝试在我的项目中实现它们,但它们都没有为我工作。

1 个答案:

答案 0 :(得分:2)

通过以下线

为您的imageview启用用户互动
imgview.userInteractionEnabled =YES;

//这是示例代码

UILongPressGestureRecognizer *gestureRecognizer = [[UILongPressGestureRecognizer alloc] init];
[gestureRecognizer addTarget:self action:@selector(imgLongPressed:)];
gestureRecognizer.delegate = self;
imgview.userInteractionEnabled =YES;
[imgview addGestureRecognizer: gestureRecognizer];

- (void) imgLongPressed:(UILongPressGestureRecognizer*)sender
{
    UIImageView *view_ =(UIImageView*) sender.view;
    CGPoint point = [sender locationInView:view_.superview];

    if (sender.state == UIGestureRecognizerStateBegan)
    {

    }
    else if (sender.state == UIGestureRecognizerStateChanged)
    {

    }
    else if (sender.state == UIGestureRecognizerStateEnded)
    {

    }

}