CollectionViewController UILongPressGestureRecognizer仅在设置延迟和最小值时起作用

时间:2016-10-12 14:01:38

标签: ios ios9

我无法理解为什么要设置

longGesture.minimumPressDuration = 0.3;
longGesture.delaysTouchesBegan = true;

action被解雇之前,你知道为什么吗?

longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
longGesture.minimumPressDuration = 0.3;
longGesture.delaysTouchesBegan = true;
[self.collectionView addGestureRecognizer:longGesture];

1 个答案:

答案 0 :(得分:0)

Try this code. You don't have to set this longGesture.minimumPressDuration = 0.3
- (void)viewDidLoad
{
    UILongPressGestureRecognizer *lpgr 
       = [[UILongPressGestureRecognizer alloc]
                     initWithTarget:self action:@selector(handleLongPress:)];
    lpgr.delegate = self;
    lpgr.delaysTouchesBegan = YES;
    [self.collectionView addGestureRecognizer:lpgr];
}

-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
    if (gestureRecognizer.state != UIGestureRecognizerStateEnded) {
        return;
    }
    CGPoint p = [gestureRecognizer locationInView:self.collectionView];

    NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:p];
    if (indexPath == nil){
        NSLog(@"couldn't find index path");            
    } else {
        // get the cell at indexPath (the one you long pressed)
        UICollectionViewCell* cell =
        [self.collectionView cellForItemAtIndexPath:indexPath];
        // do stuff with the cell`enter code here`
    }
}