2016-10-12 1 views
0

я не могу понять, почему я должен установитьCollectionViewController UILongPressGestureRecognizer работать только при установке задержки и минимальное

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

delaysTouchesBegan устанавливает, чтобы избежать срабатывания CollectionView didHighlightItemAtIndexPath Митосе. Также minimumPressDuration не требуется, поскольку значение по умолчанию равно 0,5. –

ответ

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` 
    } 
} 
Смежные вопросы