2016-10-12 3 views
0

を設定するとき、私はactionが発射されて、あなたはなぜ知っている前にCollectionViewController UILongPressGestureRecognizerだけ遅延と最小

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

を設定する必要があり、なぜ私が理解することはできません動作しますか?

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

delaysTouchesBeganはcollectionviewのdidHighlightItemAtIndexPathのmethosのトリガを避けるために設定します。 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` 
    } 
}